每当我使用以下代码时,都会出现语法错误。
print('1. Can elephants jump?')
answer1 = input()
if answer1 = 'yes':
print('Wrong! Elephants cannot jump')
if answer1 = 'no':
print('Correct! Elephants cannot jump!'
我相信它与字符串不能相等有关吗?
每当我使用以下代码时,都会出现语法错误。
print('1. Can elephants jump?')
answer1 = input()
if answer1 = 'yes':
print('Wrong! Elephants cannot jump')
if answer1 = 'no':
print('Correct! Elephants cannot jump!'
我相信它与字符串不能相等有关吗?
您正在使用赋值 (one =) 而不是相等测试 (double ==):
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!')