-1

我的 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: ")))

问题在代码中。请在您的答案中使用简单的术语,因为我只是一个初学者。

4

1 回答 1

6

您应该使用模运算符而不是除法运算符。

if number % 3 == 0:

尝试

print(9 % 3)
print(9 / 3)

你会得到

0
3.0

模运算符返回除法的余数,除法运算符返回除法的商。

于 2013-11-06T10:41:14.110 回答