Python if / else和elif

时间:2016-12-18 04:17:26

标签: python if-statement syntax-error

当我运行下面的代码时,我得到一个SyntaxError。任何人都可以帮我解决这个问题。谢谢

mien = 'beautiful' 

if mien = 'stupid':
   print ('you are stupid')
   else:
       if mien = 'beautiful':
       print ('you are right, thank you!')
       else: #line16
           print ('Opps!')

以下是错误消息:

>File "..\Playground\", line 16     
>    else:     
>       ^     
>SyntaxError: invalid syntax 

2 个答案:

答案 0 :(得分:1)

使用==运算符测试相等性。此外,您可以使用elif关键字,如下所示:

mien = 'beautiful'
if mien == 'stupid':
    print('you are stupid')
elif mien == 'beautiful':
    print('you are right, thank you!')
else:
    print('Opps!')

答案 1 :(得分:0)

mien = 'beautiful'
if mien == 'stupid':
    print ('you are stupid')
else:
    if mien == 'beautiful':
        print ('you are right, thank you!')
    else:
        print ('Opps!')

你忘了使用" =="在IF声明中进行比较。

enter image description here