Kab*_*bra -1 python if-statement python-3.x
我的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: ")))
Run Code Online (Sandbox Code Playgroud)
问题出在代码中.请在答案中使用简单的术语,因为我只是初学者.
您应该使用模运算符而不是除法运算符.
if number % 3 == 0:
Run Code Online (Sandbox Code Playgroud)
尝试
print(9 % 3)
print(9 / 3)
Run Code Online (Sandbox Code Playgroud)
你会得到
0
3.0
Run Code Online (Sandbox Code Playgroud)
模运算符返回除法的余数,除法运算符返回除法的商.