无法得到我的输入重复或d

2024-06-26 14:39:12 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,所以我要做的是让这个程序要求用户输入退出,随机运行,或者删除已经绘制的内容,然后再次随机运行

我有运行周围的部分下来罚款,我需要帮助我的输入(称为“消息”),以实际运行选项“w”和“e”到目前为止,它所做的是绘制一个新的随机运行我可能只是搞错了海龟的命令,我想不出来)。我相信我用错了词(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)

Tags: ofthetomessageifcount绘制steps
1条回答
网友
1楼 · 发布于 2024-06-26 14:39:12

来自randomrunreturn被忽略。无论如何,重复输入提示是个坏主意。从randomrun中删除它和return,并以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.")

相关问题 更多 >