首页 > 解决方案 > python3中else语句的语法无效

问题描述

当我尝试运行我的代码时,它说我的 else 语句的语法无效,但我不知道出了什么问题。

import random
import time
username = input("Hello.  Please enter your name, then press 'enter'.  
After you type something, you will need to /n"
                 "click the 'enter' key to send it")
print ("Hello " + username)
time.sleep(3)
game_tutorial_input = input("Do you wish to see the tutorial? (y/n)")
if game_tutorial_input == "y":
    print ("Great!  Press enter after each instruction to move /n" 
"onto the next one.")
    else
        print("Are you sure? (y/n")

标签: pythonpython-3.xsyntax

解决方案


缩进在 python 中很重要,确保else与 .indent 在同一缩进列上if。并且如前所述,您需要 a:else.. 的末尾,即else:


推荐阅读