无法将变量转换为整数(由于if语句?)

时间:2019-05-19 11:18:38

标签: python

抱歉,我正在编写一个程序,用于解决过去的纸质问题,以查找用户输入的数字是否为质数,然后循环播放。

问题是我似乎无法在if语句中将输入usernofloat转换为int

任何帮助将不胜感激

run = True

while run == True:

    userno = float(input("Please enter a number to check if it's a prime number."))

    if userno < 1:
        print ("The number entered was lower than 1, so can't be prime.")
    else:
        int(userno)                         #this doesn't seem to work
        for norange in range (2,userno):    #userno is still a float here
            if userno % norange == 0:
                print ("This is not a prime number, sorry.")
            else:
                print ("This is a prime number. :)")

    runagain = input ("Do you want to enter another number? Please enter 'yes' or 'no'.")
    if runagain == "no":
        run = False

1 个答案:

答案 0 :(得分:0)

int(userno)以整数形式返回userno,但不会覆盖userno实际变量的值。

您需要使用userno = int(userno)