Jun-02-2020, 08:06 PM
Hello, i am new to Python so i couldn't figure out how to check the input.
I want to get an input from the user(integer or float) and want to give an error message when it enters anything else(letter or any special character).
So far i know this works for the integer:
But if i want it to accept decimal numbers as well integers what should i write? (very basically cause i am new)
Here are some of the things i tried which none of them worked.
1)
I want to get an input from the user(integer or float) and want to give an error message when it enters anything else(letter or any special character).
So far i know this works for the integer:
while True : try: x = int(input('please enter an integer: ')) print(x) break except ValueError: print('not valid')First can someone please explain the logic of this. I didn't understand it thoroughly.
But if i want it to accept decimal numbers as well integers what should i write? (very basically cause i am new)
Here are some of the things i tried which none of them worked.
1)
radius= input("enter a number: ") if isinstance(radius,float): print(radius) elif isinstance (radius,int): print(radius) else: print("Not a number")2)
radius= input("enter number: ") if isinstance(radius,numbers.Real): print(radius) else: print("not number")3)
radius= input("enter number: ") if type(radius) in (float,int): print(radius) else: print("not a number")4)
x=input('please enter integer: ') if type(x) is int: print(x) elif type(x) is float: print(x) else: print('error')5)
x=input('please enter integer: ') if not type(x) is int: raise TypeError('only numbers allowed')