对不起,但是我正在为过去的纸质问题编写一个程序,以查找用户输入的号码是否为素数,然后循环。
问题是,我似乎无法将输入userno
从if语句中的float
转换为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
int(userno)
返回用户诺作为整数,但不会覆盖userno
实际变量的值。
您需要使用userno = int(userno)