0

while loop

How can the while loop be written better in the code snippet below, or do you think there is an error? #code requirements: #if character is not letter, it is requested to enter text consisting of letters again T = True while T: text = input("enter a text consisting of only letters: ") for i in text: if not i.isalpha(): text = input("enter a text consisting of only letters: ") break else: T = False

7th Dec 2021, 6:08 PM
Bekir
Bekir - avatar
5 Réponses
+ 3
No extra loop required here: while True: text = input("Enter a text consisting of only letters.\n") if text.isalpha(): print("Your input:",text); break else: print("You entered a number. ")
7th Dec 2021, 8:03 PM
Solo
Solo - avatar
+ 2
Bekir no thanks, you can just mark my answer as the best ☺️
7th Dec 2021, 9:55 PM
Solo
Solo - avatar
+ 1
Bekir your posted code would prompt for input three times if a non-alpha char gets entered once at the first prompt. Input from the second prompt would be ignored. Here is a correction: T = True while T: text = input("enter a text consisting of only letters: ") for i in text: T = not i.isalpha() if T: break
7th Dec 2021, 7:05 PM
Brian
Brian - avatar
+ 1
Thanks Vasily. I never thought of it this way.
7th Dec 2021, 8:38 PM
Bekir
Bekir - avatar
0
Thanks Brain. Yes, I saw the error you mentioned. But I don't understand my code error. But I'm trying to understand. Also your code is really good.
7th Dec 2021, 7:28 PM
Bekir
Bekir - avatar