Python User Must Only Enter Float Numbers
I am trying to find out how to make it so the user [only enters numbers <0] and [no letters] allowed. Can someone show me how I would set this up. I have tried to set up try/cat
Solution 1:
I think you want numbers only > 0, or do you want less than zero? Anyway to do this you can using try/except, inside a while loop.
For Python 3, which I think you are using?
goodnumber = Falsewhilenot goodnumber:
try:
edgeone = int(input('Enter the first edge of the triangle:'))
if edgeone > 0:
print('thats a good number, thanks')
goodnumber = Trueelse:
print('thats not a number greater than 0, try again please')
except ValueError:
print('Thats not a number, try again please')
hope this helps.
Solution 2:
you must use except handler for this case :
try:
value = int(raw_input("Enter your number:"))
ifnot ( value < 0 ):
raise ValueError()
except ValueError:
print"you must enter a number <0 "else:
print value #or something else
Also This question already has an answer here: Accepting only numbers as input in Python
Solution 3:
You can do like this.
i = 1while(type(i)!=float):
i=input("enter no.")
try:
int(i):
except:
try:
i = float(i)
except:
passprint(i)
Post a Comment for "Python User Must Only Enter Float Numbers"