似乎无法使我的其他陈述在python-turtle中正常工作

发布于 2025-01-30 09:11:05 字数 2260 浏览 1 评论 0原文

抱歉,我有点是初学者,我很难让其他陈述在这个程序中正常工作,可以与Python一起绘制乌龟。我尝试缩进Else语句else:print(“错误输入!”),但随后只是说“错误输入!”每次我做某事;该程序运行良好,但这会不断重复。如果我保留这样的其他陈述,并且我输入了其他任何内容,则“错误输入!”应该不会出现这种弹出。有人知道该怎么办吗?

例如: 如果我像在这里给它一样运行代码,这就是结果:

想做什么?

您 前进?: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()

sorry I am a bit of a beginner and I am having trouble gettin the else statement working properly in this program i made to draw stuff in turtle with python. I tried indenting the else statementelse: print("Wrong input!")but then it just says "Wrong input!" Everytime i do something; the program works fine but that keeps repeating. If i keep the else statement like that and I type anything else then the "Wrong input!" that is supposed to pop up doesn't appear. Anyone know what to do?

For example:
If i run the code like how i gave it here this is how it turns out:

what you want to do?: f (<-- I input this)

How many pixels you want to go forward?: 100 (<-- I input this)

and now if i put something else

what you want to do?: asbfaifb (<-- I input this)

what you want to do?:

it doesnt show the "Wrong input!" message that is supposed to say

On the other hand if I indent the else statement one more time this is the outcome

What you want to do?: f

How many pixels you want to go forward?: 100

Wrong input!

What you want to do?: r

How many degrees you want to turn right?: 90

Wrong input!

What you want to do?: 100

Wrong input!

What you want to do?: b

Wrong input!

What you want to do?:

but the code still runs fine, it just keeps repeating the "Wrong input!" over and over

#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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妞丶爷亲个 2025-02-06 09:11:05

else遵循最后的,如果或elif语句。对于您来说,这是:

如果用户==“ stop”:

这意味着,如果用户不是“停止”,则告诉程序打印“错误输入”。

一个简单的修复是简单地将更改为,如果user ==“停止”: elif user ==“ stop”:

else follows the last if or elif statement before it. For you, this is:

if user == "stop":

Which means, if user is not "stop", you are telling the program to print "wrong input".

An easy fix is to simply change if user == "stop": to elif user == "stop":

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文