不好意思,我是一个初学者,我有困难,使其他语句在这个程序中正常工作,我做了用蟒蛇在海龟里画东西。我试着缩进else: print("Wrong input!")
语句,但是它只是说“输入错误!”每次我做某件事时,程序都很好,但一直在重复。如果我保留了the语句,并且键入了其他任何内容,那么“输入错误!”应该会弹出的东西不会出现。有人知道该怎么做吗?
例如:如果我像我在这里给它的那样运行代码,结果就是这样的:
您想做什么?:f (<--我输入此)
你想前进多少像素?:100 (<-我输入这个)
现在如果我放点别的东西
您想做什么?:asbfaifb (<-我输入这个)
你想做什么?:
它没有显示“错误的输入!”应该说的信息
另一方面,如果我再缩进另一条语句,这就是结果
你想做什么?:f
你想前进多少像素?:100
输入错误!
你想做什么?:r
你想向右转多少度?:90
输入错误!
你想做什么?:100
输入错误!
你想做什么?:b
输入错误!
你想做什么?:
但是代码仍然运行良好,它只是不断地重复“错误的输入!”再三
#importing turle
import turtle
#Creating our turtle and naming it
turtle = turtle.Turtle()
#Ask the user what to do and tell the instructions
#Make a function for the instructions
def instructions():
print("What do you want me to do? Please choose the letter corresponding to the action: ")
print("Forward = f")
print("Turn right = r")
print("Turn left = l")
print("Turn backwards = b")
print("If you want to stop: stop\n")
#print out the instuctions
instructions()
#Function for drawing by getting the user's input
def drawing():
while True:
user = input("What you want to do?: ")
#If they want to go forward
if user == "f":
l = int(input("How many pixels you want to go forward?: "))
turtle.forward(l)
#If they want to turn right
elif user == "r":
x = int(input("How many degrees you want to turn right?: "))
turtle.right(x)
#If they want to turn left
elif user == "l":
y = int(input("How many degrees you want to turn left?: "))
turtle.left(y)
#If they want to turn backwards
elif user == "b":
z = 180
turtle.right(z)
#If they want to stop the program
if user == "stop":
print("\nOk, I will stop the program now\n\nTo make a new drawing, re-run the program")
break
#If they type anything else
else:
print("Wrong input!")
drawing()
发布于 2022-05-19 03:11:27
else
遵循它之前的最后一个if
或elif
语句。对你来说,这是:
if user == "stop":
这意味着,如果用户不是“停止”,你是告诉程序打印“错误的输入”。
一个简单的解决方法是简单地将if user == "stop":
更改为elif user == "stop":
https://stackoverflow.com/questions/72298172
复制相似问题