+ 1

Whys this not working?

Only letters are allowed as input, i intentionally typed in a number but didnt get wrong https://code.sololearn.com/c7PoEd2A8I1X/?ref=app

26th Feb 2022, 7:19 AM
Lenoname
7 Réponses
+ 5
Lenoname Raise exception only when input is not alpha try: a = input() b = a.isalpha() if not b: raise Exception except Exception: print("wrong") You can also do this if you don't want numeric values try: a = input() print (int(a)) except Exception: print("Enter only string")
26th Feb 2022, 2:22 PM
A͢J
A͢J - avatar
+ 2
You need an if for isalpha, so something like this: try: a = input() b = a.isalpha() if not b: raise Exception except Exception: print("wrong") But you really don't need the try except for this.
26th Feb 2022, 8:12 AM
Paul
Paul - avatar
+ 1
The value of b will be TRUE or FALSE. But it will not be an exception.
26th Feb 2022, 7:49 AM
Paul
Paul - avatar
+ 1
You need something like this: a = input() b = a.isalpha() if b: print('Good') else: print('Wrong input')
26th Feb 2022, 7:56 AM
Paul
Paul - avatar
+ 1
I’m trying to use exceptions
26th Feb 2022, 8:07 AM
Lenoname
0
try: a = input() b = a.isalpha() raise Exception except Exception: print("wrong") ??like this? Printed wrong every time even when i used alphabets
26th Feb 2022, 7:59 AM
Lenoname
0
Paul how about this one?⬆️
26th Feb 2022, 8:06 AM
Lenoname