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
5 odpowiedzi
+ 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. ")
+ 2
Bekir no thanks, you can just mark my answer as the best ☺️
+ 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
+ 1
Thanks Vasily. I never thought of it this way.
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.