0

Detecting White Spaces, Empty Input Variables and Non numera values in Python

Am trying to make python detect an empty user input and a non-number input so that it can throw an error and exit the program. But the code that am using here is not capturing the white spaces, its throwing an EOF error for an empty input. If someone can add code to detect non-numeral values, i would greatly appreciate it... num1=float(input("Enter the dividend?\n")) num2=float(input("Enter the divider\n")) if(num1==" " or num2==" "): print("You never submitted an input") else: try: num3=(num1/num2) print(num3) except: print("Zero Error Division")

15th Aug 2020, 3:37 PM
Timothy Njiru
Timothy Njiru - avatar
4 Respuestas
+ 5
ip = input() try: ip = int(ip) : except: try: ip = float(ip) except : print("no number") Lothar u thing thats better?
15th Aug 2020, 4:03 PM
Oma Falk
Oma Falk - avatar
+ 1
Well, since you are using the float function to convert the string from input to float, there is no need for the check because Python will throw an error if the input is not a number in general. You just catch the error and simply ask for only number inputs. If you want to throw an error if the input is empty, you can do something like: if not (num1 and num2): ... Here one or both of the inputs is an empty string which evaluates to False, in that case, "not" makes the case True. Otherwise, "not" will always make it False.
15th Aug 2020, 3:50 PM
Ali Abdelhady
Ali Abdelhady - avatar
0
Thanks so much @Ali, am gonna implement that right now, i asked about the white spaces because i figured google uses Python for back end and i needed codr to throw errors on white spaces but thanks
15th Aug 2020, 3:59 PM
Timothy Njiru
Timothy Njiru - avatar
0
@Falik, On it Falik
15th Aug 2020, 4:04 PM
Timothy Njiru
Timothy Njiru - avatar