我的if / else语句遇到了一些问题。这是我的代码,问题在代码本身的多行注释中描述:
def cube(*number):
return number**3
def by_three(number):
if number/3==0: #Checks if number is divisible by 3.
cube_of=cube(number) #Cubes number.
print(cube_of) #Prints cube of number.
return False
else:
print("Your number isn't divisible by three. Try again.") #Comment is below
"""
Tells if number is not divisible by three. If I give 9 as the argument,
this block is executed. Why so?
"""
return True
while True:
by_three(int(input("Give me any number divisible by three: ")))
问题出在代码中。请在答案中使用简单的术语,因为我只是初学者。
答案 0 :(得分:6)
你应该使用modulo operator not division operator。
if number % 3 == 0:
尝试
print(9 % 3)
print(9 / 3)
你会得到
0
3.0
模运算符返回除法的余数,除法运算符返回除法的商。