我正试图这样做,如果你输入一个字母/非数字,你得到“请插入一个数值......”但它似乎没有工作......
#This allows for the user to input the values of the variables
a = float(raw_input ("Please insert a numerical value for a: "))
while not (a.isdigit()):
a = float(raw_input ("Please insert a numerical value for a: "))
print ("Well done.")
b = float(raw_input ("Please insert a numerical value for b: "))
while not (b.isdigit()):
b = float(raw_input ("Please insert a numerical value for b: "))
print ("Well done.")
c = float(raw_input ("Please insert a numerical value for c: "))
while not (b.isdigit()):
c = float(raw_input ("Please insert a numerical value for c: "))
print ("Well done.")
答案 0 :(得分:1)
isdigit()
是字符串的方法,你在这里使用浮点数。
答案 1 :(得分:1)
isdigit()
用于字符串。你正在使用花车,并且施法可以提升ValueError
。所以这样做:
while True:
try:
c = float(raw_input("Enter a numerical value")
break
except ValueError:
pass
答案 2 :(得分:0)
您正在isdigit()
对象上调用float
。基本上,您需要在输入字符串上调用isdigit()
之前调用float()
。以下是我可以编写一个循环的方法:
while True:
s = raw_input ("Please insert a numerical value for a: ")
try:
a = float(s)
break
except ValueError:
print "Please enter a numeric value."
print ("Well done.")
答案 3 :(得分:-1)
没有关于while循环的错误,但错误是ondigit()函数,这是用于字符串...所以首先你输入将其转换为字符串然后使用函数检查它