If 语句 - 在 Python 中使用模数运算符的嵌套条件
If Statement - Nested Conditional Using Modulus Operator in Python
我需要在 Python 中创建一个程序,要求用户输入一个数字,然后告诉用户该数字是否为偶数或是否可以被 5 整除。如果两者都不成立,则不打印任何内容.例如:
Please enter a number: 5
This number is divisible by 5!
Please enter a number: 7
Please enter a number: 20
This number is even!
This number is divisible by 5!
我试图复制 this answer 中使用的方法,但我在第 8 行收到错误消息:
SyntaxError: invalid syntax (<string>, line 8) (if Num_1 % 2 == 0)
这是我的代码:
#TODO 1: Ask for user input
Num1 = input("Please enter a number")
#TODO 2: Turn input into integer
Num_1 = int(Num1)
#TODO 2: Use conditionals to tell the user whether or not their
#number is even and/or divisible by 5
if Num_1 % 2 == 0
print ("This number is even!")
if Num_1 % 5 == 0
print ("This number is divisible by 5!")
因为我使用模数运算符来确定 Num_1 是否是 2 的精确倍数,所以我应该返回一个 True 值,因此应该打印 "This number is even!" 但是我却相反收到此错误消息 - 为什么?谢谢!
每个 Python 块的开头应以冒号 :
结尾。还要注意缩进。
Num1 = input("Please enter a number")
Num_1 = int(Num1)
if Num_1 % 2 == 0:
print ("This number is even!")
if Num_1 % 5 == 0:
print ("This number is divisible by 5!")
我需要在 Python 中创建一个程序,要求用户输入一个数字,然后告诉用户该数字是否为偶数或是否可以被 5 整除。如果两者都不成立,则不打印任何内容.例如:
Please enter a number: 5
This number is divisible by 5!
Please enter a number: 7
Please enter a number: 20
This number is even!
This number is divisible by 5!
我试图复制 this answer 中使用的方法,但我在第 8 行收到错误消息:
SyntaxError: invalid syntax (<string>, line 8) (if Num_1 % 2 == 0)
这是我的代码:
#TODO 1: Ask for user input
Num1 = input("Please enter a number")
#TODO 2: Turn input into integer
Num_1 = int(Num1)
#TODO 2: Use conditionals to tell the user whether or not their
#number is even and/or divisible by 5
if Num_1 % 2 == 0
print ("This number is even!")
if Num_1 % 5 == 0
print ("This number is divisible by 5!")
因为我使用模数运算符来确定 Num_1 是否是 2 的精确倍数,所以我应该返回一个 True 值,因此应该打印 "This number is even!" 但是我却相反收到此错误消息 - 为什么?谢谢!
每个 Python 块的开头应以冒号 :
结尾。还要注意缩进。
Num1 = input("Please enter a number")
Num_1 = int(Num1)
if Num_1 % 2 == 0:
print ("This number is even!")
if Num_1 % 5 == 0:
print ("This number is divisible by 5!")