How to allow only letters as input? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to allow only letters as input?

A = input(”your input: ”) If A == None: print(”wrong try again”) This wont work

26th Feb 2022, 4:56 AM
Lenoname
10 Answers
+ 7
try-except: from string import ascii_letters try: a = input() assert all(c in ascii_letters for c in a) except: print("Wrong, try again") isalpha(): a = input() if a.isalpha(): print("Correct!") else: print("Wrong")
26th Feb 2022, 10:49 AM
Simba
Simba - avatar
+ 5
isalpha() method or try-except statement, not both
26th Feb 2022, 8:31 AM
Simba
Simba - avatar
+ 5
Lenoname , what about this: while not (inp := input()).isalpha(): print("wrong input - try again: ") print("all chars are letters:", inp) it does not handle spaces, but this was not mentioned
26th Feb 2022, 12:03 PM
Lothar
Lothar - avatar
+ 3
🇮🇳SATYAPRAKASH🇮🇳 that accomplishes literally nothing. All input is string in python.
26th Feb 2022, 5:32 AM
Simon Sauter
Simon Sauter - avatar
+ 2
try: a = input() assert a.isalpha() print(a) except: print("wrong")
26th Feb 2022, 8:05 AM
Simon Sauter
Simon Sauter - avatar
+ 2
take input while incorrect input: ask again if the test is really bulky you might instead wanna do: while True: take input if correct input: break else: complain some to the user
27th Feb 2022, 4:27 AM
Vaibhav
Vaibhav - avatar
0
Simon Sauter this didnt work though… any suggestions? https://code.sololearn.com/c7PoEd2A8I1X/?ref=app
26th Feb 2022, 6:51 AM
Lenoname
- 1
Try-except without isalpha?Could u give an example please
26th Feb 2022, 8:41 AM
Lenoname
- 2
...All input is string in python.
27th Feb 2022, 1:32 PM
Owoeye Olorunfemmi
Owoeye Olorunfemmi - avatar