好吧,所以我想做的就是让该程序询问用户退出,随机运行或擦除已绘制内容并再次随机运行的输入。
我的运行部分正常运行,我需要帮助来获得输入(称为“消息”)以实际运行选项“ w”和“ e”,到目前为止,它所做的只是绘制一个新的随机运行。(我可能只是输入了错误的turtle命令,无法弄清楚)。我相信我使用了错误的单词(if,while,elif)来使菜单正常工作。我还认为jumpto函数无法正常工作,或者正在其他函数中对其进行重置。
import turtle as t
import random as r
count=0
t.speed(0)
x=r.randint(1,100)
y=r.randint(1,100)
#----------------------------------------
""" sets the turtle to a new starting point"""
def jumpto(x,y):
t.penup()
t.goto(x,y)
t.pendown()
return None
def randomrun ():
"""runs turtle around 1000 steps randomly"""
count=0
while count <1000:
count+=1
t. forward (6)
t.left(r.randint(0,360))#360 degree choice of rotation
t.dot(10)#puts a dot at the end of the run of lines
count=0#resets count so it can do it again
x=r.randint(1,100)
y=r.randint(1,100)
message= input("q to quit \nw to walk randomly for 1000 steps \ne to erase screen and walk randomly ")
return message
#-------------------------------------------
message= input("q to quit \nw to walk randomly for 1000 steps \ne to erase screen and walk randomly ")
if message =="w":
randomrun()
jumpto(x,y)
if message == "q":
print(" have a nice day")
if message== "e":
t.clear()
randomrun()
jumpto(x,y)
在return
从randomrun
被忽略。无论如何,重复输入提示是一个坏主意。删除它,然后将其return
从中删除randomrun
并以input
循环结尾。
while True:
message = input("q to quit\n" # use implicit string joining
"w to walk randomly for 1000 steps\n"
"e to erase screen and walk randomly\n"
"> ")[:1].lower() # forgive non-exact input
if message == "q":
print("Have a nice day!")
break
elif message =="w":
randomrun()
jumpto(x,y)
elif message == "e":
t.clear()
randomrun()
jumpto(x,y)
else:
print("Input not recognized; try again.")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句