这个简单的代码给了我语法上的错误,除数代码

问题描述 投票:-4回答:2
def main():
    user_number = float(input("Enter a number: "))
    num = user_number//2
    round(num,0)
    int(num)
    print(num)
    divisor = 1
    for divisor in range(divisor,num):
        if num%divisor == 0:
                print(divisor)

while True:
    main()

我得到的语法错误:TypeError:'float'对象无法解释为整数

python int rounding
2个回答
0
投票

num = user_number // 2的结果是浮点数。

调用round(num, 0)不执行任何操作,因为您没有将结果存储在num变量中。无论如何,结果是float。您应该改写num = int(num)


0
投票

您应该改写for divisor in range(divisor, int(num))

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