0

I'm attempting to accept user input and check the string for non-alphabet values. My problem is if they enter a bad value, how do I query them again and start the loop over? See below

name = str(input("Enter name:"))

for i in name:
     if not i.isalpha():
          name = str(input("Enter name:")
          **line to start iterating from the beginning with new entry.**

Just trying to verify users only enter letters. If the check fails they enter the name again and it starts over. Thanks in advance!

milanbalazs
  • 4,811
  • 4
  • 23
  • 45
Marlz129
  • 21
  • 2

2 Answers2

0

You can do something like this:

correct = False
while correct == False:

    name = str(input("Enter name:"))
    for i in name:
        if not i.isalpha():
            correct = False
            break
        else:
            correct = True
Paolo Mossini
  • 1,064
  • 2
  • 15
  • 23
0

You can see below an example code:

while True:
    number_found = False
    name = str(input("Enter name:"))
    for i in name:
        print("Check {} character".format(i))
        if i.isdigit():
            print("{} is number. Try again.".format(i))
            number_found = True
            break  # Break the for loop when you find the first non-alpha. You can reduce the run-time with this solution.
    if not number_found:
        break
print("Correct input {}".format(name))

Output:

>>> python3 test.py # Success case
Enter name:test
Check t character
Check e character
Check s character
Check t character
Correct input test

>>> python3 test.py # Failed case
Enter name:test555
Check t character
Check e character
Check s character
Check t character
Check 5 character
5 is number. Try again.
Enter name:
milanbalazs
  • 4,811
  • 4
  • 23
  • 45