Python 中的简单 If 程序语法错误

问题描述 投票:0回答:2
num = int(input("Enter a number: ")
if num%2 == 0:
    print("The number is even")

else:
    print("The number is odd")enter code here

我在 if 语句行上遇到语法错误,我到处搜索了发生这种情况的原因,但我就是找不到它,帮忙吗?如果有帮助的话,我正在使用 Visual Studio 2015 作为我的 IDE。

python python-3.x if-statement syntax
2个回答
1
投票
num = int(input("Enter a number: "))
if num%2 == 0:
    print("The number is even")

else:
    print("The number is odd")enter code here

前面一行缺少 )。很多时候,当您在一行上遇到语法错误时,它会发生在该行或一行或几行之前,在您的情况下,它正在寻找匹配的括号。


0
投票

@mikeb
所述,错误在上一行,其中
num = int(input("Enter a number: ")
应该是
num = int(input("Enter a number: "))

当您查找错误时,请务必检查上一行。

© www.soinside.com 2019 - 2024. All rights reserved.