每当我使用下面的代码,它给我一个语法错误。输入错误Python 3.3
print('1. Can elephants jump?')
answer1 = input()
if answer1 = 'yes':
print('Wrong! Elephants cannot jump')
if answer1 = 'no':
print('Correct! Elephants cannot jump!'
我认为这与一个字符串有什么关系不能相等的东西?
每当我使用下面的代码,它给我一个语法错误。输入错误Python 3.3
print('1. Can elephants jump?')
answer1 = input()
if answer1 = 'yes':
print('Wrong! Elephants cannot jump')
if answer1 = 'no':
print('Correct! Elephants cannot jump!'
我认为这与一个字符串有什么关系不能相等的东西?
您正在使用分配(一个=
),而不是平等的测试(双==
):
if answer1 = 'yes':
和
if answer1 = 'no':
双倍=
到==
:
if answer1 == 'yes':
和
if answer1 == 'no':
您还缺少一个右括号:
print('Correct! Elephants cannot jump!'
末添加缺少的)
。
你在最后print
缺少一个右括号:
print('Correct! Elephants cannot jump!')
# here--^
此外,您还需要使用==
进行对比测试,而不是=
(这是变量赋值)。
最后,您应该使用elif
来测试某件事或另一件事。
更正代码:
print('1. Can elephants jump?')
answer1 = input()
if answer1 == 'yes':
print('Wrong! Elephants cannot jump')
elif answer1 == 'no':
print('Correct! Elephants cannot jump!')
谢谢,我要求一个更正,并得到3,感谢您的帮助! – user2913135
使用==进行比较。不=,那是分配。
您可能还需要检查你的()
它始终是一个好主意,张贴在您的文章中的错误消息太 – ModulusJoe
你在第一行有一个'IndentationError',它阻止你甚至到达第一个'SyntaxError'。 – abarnert