-3

每当我使用以下代码时,都会出现语法错误。

    print('1. Can elephants jump?')
answer1 = input()
if answer1 = 'yes':
    print('Wrong! Elephants cannot jump')
if answer1 = 'no':
    print('Correct! Elephants cannot jump!'

我相信它与字符串不能相等有关吗?

4

3 回答 3

3

您正在使用赋值 (one =) 而不是相等测试 (double ==):

if answer1 = 'yes':

if answer1 = 'no':

加倍===

if answer1 == 'yes':

if answer1 == 'no':

您还缺少右括号:

print('Correct! Elephants cannot jump!'

)在末尾添加缺少的内容。

于 2013-10-23T22:41:38.910 回答
2

使用 == 进行比较。不是 = ,那是为了赋值。

您可能还想检查您的 ()

于 2013-10-23T22:43:25.180 回答
2

您在最后缺少一个右括号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!')
于 2013-10-23T22:41:39.327 回答