首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >似乎无法使我的感应器语句在Python-turtle中正常工作。

似乎无法使我的感应器语句在Python-turtle中正常工作。
EN

Stack Overflow用户
提问于 2022-05-19 03:07:23
回答 1查看 17关注 0票数 0

不好意思,我是一个初学者,我有困难,使其他语句在这个程序中正常工作,我做了用蟒蛇在海龟里画东西。我试着缩进else: print("Wrong input!")语句,但是它只是说“输入错误!”每次我做某件事时,程序都很好,但一直在重复。如果我保留了the语句,并且键入了其他任何内容,那么“输入错误!”应该会弹出的东西不会出现。有人知道该怎么做吗?

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

您想做什么?:f (<--我输入此)

你想前进多少像素?:100 (<-我输入这个)

现在如果我放点别的东西

您想做什么?:asbfaifb (<-我输入这个)

你想做什么?:

它没有显示“错误的输入!”应该说的信息

另一方面,如果我再缩进另一条语句,这就是结果

你想做什么?:f

你想前进多少像素?:100

输入错误!

你想做什么?:r

你想向右转多少度?:90

输入错误!

你想做什么?:100

输入错误!

你想做什么?:b

输入错误!

你想做什么?:

但是代码仍然运行良好,它只是不断地重复“错误的输入!”再三

代码语言:javascript
运行
复制
#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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-19 03:11:27

else遵循它之前的最后一个ifelif语句。对你来说,这是:

if user == "stop":

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

一个简单的解决方法是简单地将if user == "stop":更改为elif user == "stop":

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72298172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档