I'm working on a silly page for my friends, however when the code askes for user input I am incapable of entering any data. How can I possibly fix this?
# import the turtle module
import turtle as trtl
troll = trtl.Turtle()
clr = input("give a color pls: ")
bgcolor(clr)
wn = trtl.Screen()
wn.mainloop()
2 Answers 2
Without seeing the rest of the code, I think the most likely problem is doing all of this before you have the screen initialized. After all, if the screen has not been created yet, where would you expect the input to appear? Here is how I would change it:
import turtle as trtl
troll = trtl.Turtle()
# Create the screen first
wn = trtl.Screen()
clr = input("give a color pls: ")
# properly use bgcolor as a function from turtle, not a standalone function
wn.bgcolor(clr)
wn.mainloop()
Comments
Based on @KRoscoe45's answer but with a bit of error handling :
import turtle as trtl
troll = trtl.Turtle()
color_list=["blue","red","pink","black","white"]
wn = trtl.Screen()
clr = input("give a color pls: ")
while not crl in color_list :
input(f"Color must be part of this list :\n{color_list}.\nPlease reenter one:\t")
wn.bgcolor(clr)
wn.mainloop()
This would handle error caused if the user dosn't enter a color that is understandable by the turtle module by reasking for a color until the answer is part of color_list.
Comments
Explore related questions
See similar questions with these tags.
input()gets its data from the terminal where the script is running.