I am trying to make a program that asks the user to draw a shape and how many of that shape to draw in Python turtle. I dont know how to make the dialogue box so the user can say how many to add and make it run correctly. Any help will be awesome! Here is my code so far:
import turtle
steps = {"triangle": 3, "square": 4, "pentagon": 5, "hexagon": 6, "octagon": 7}
#this is the dialogue box for what shape to draw and moving it over a bit so the
#next shape can be seen
def onkey_shape():
shape = turtle.textinput("Enter a shape", "Enter a shape: triangle,
square, pentagon, hexagon, octagon")
if shape.lower() in steps:
turtle.forward(20)
set_shape(shape.lower())
turtle.listen()
def set_shape(shape):
global current_shape
turtle.circle(40, None, steps[shape])
current_shape = shape
turtle.onkey(onkey_shape, "d")
turtle.listen()
turtle.mainloop()
Just as you used
textinput()
to get your shape, you can usenuminput()
to get your count of how many shapes:Here's a rework of your code, which for example purposes just draws concentric shapes -- you draw them where you want them:
The tricky part, that you figured out, is that normally we only call
turtle.listen()
once in a turtle program but invokingtextinput()
ornuminput()
switches the listener to the dialog box that pops up so we need to explicitly callturtle.listen()
again after the dialogs finish.