gpt4 book ai didi

python - if/elif 语句和turtle 命令出错

转载 作者:太空宇宙 更新时间:2023-11-03 16:04:05 24 4
gpt4 key购买 nike

我在 if/else 语句中遇到了一个奇怪的错误。由于某种原因,当我使用此代码时( turtle 图形介绍项目)

from turtle import *
print('Hello. I am Terry the Turtle. Enter a command to make me draw a line, or ask for help.')
on = True
shape('turtle')
if on == True:
c1 = str(input('Enter command: ')
if c1 == 'forward':
c2 = eval(input('How many degrees? ' ))
right(c2)
elif c1 == 'right':
c2 = eval(input('How many degrees? ' ))
right(c2)
elif c1 == 'left':
c2 = eval(input('How many degrees? ' ))
left(c2)
elif c1 == 'goodbye':
print('Bye!')
on = False
else:
print('Possible commands: forward, right, left, goodbye.')

出于某种奇怪的原因,ifelif 语句不断返回语法错误,但它们似乎没有任何可见错误。我尝试做类似的事情,但它不断返回语法错误。有什么办法可以解决这个问题吗?

抱歉,如果这是一个愚蠢的问题,这是我第一次来这里,我真的很困惑。

最佳答案

我想这就是你想要的

from turtle import *
print('Hello. I am Terry the Turtle. Enter a command to make me draw a line, or ask for help.')
on = True
shape('turtle')
# Get commands until user enters goodbye
while on:
c1 = str(input('Enter command: '))
if c1 == 'forward':
# Ask how far when moving
c2 = int(input('How far? '))
# Use forward to move
forward(c2)
elif c1 == 'right':
c2 = int(input('How many degrees? '))
right(c2)
elif c1 == 'left':
c2 = int(input('How many degrees? '))
left(c2)
elif c1 == 'goodbye':
print('Bye!')
on = False
else:
print('Possible commands: forward, right, left, goodbye.')

这应该使用 Python3 运行(例如 python3 myrtle.py)。

关于python - if/elif 语句和turtle 命令出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40027925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com