【问题标题】:Why am I getting a syntax error in this piece of code?为什么在这段代码中出现语法错误?
【发布时间】:2020-11-06 12:41:24
【问题描述】:

我的程序无法运行,因为它说它有语法错误。

answer1 = input("yes or no ");

if answer1 == "yes" :
    {
        
        print("Welcome to <<A Random Chance>>")
        print("Today we will be playing <<Who's Higher>>")

【问题讨论】:

  • Python 不将 {} 用于代码块。相反,它使用缩进。
  • 也去掉那个分号。语句以新行结束。

标签: python python-3.x


【解决方案1】:

首先 python 不使用{ } 用于语句和循环。它使用:(冒号)和indentation。

第二 python 不使用分号。语句以新行结束。 (如果您希望将多个语句放在同一行上,可以使用分号分隔语句。)

所以,试试这个代码:

answer1 = input("yes or no ")

if answer1 == "yes":
    print("Welcome to <<A Random Chance>>")
    print("Today we will be playing <<Who's Higher>>")

【讨论】:

    猜你喜欢
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多