Python Forum
Can't break out of loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't break out of loops
#1
Newbie, trying to complete this homework assignment. I keep looping through the first name and not moving to the next section. I don't know what I did wrong. Below is my code.

def problem5_2():
    import re

    # first name
    while True:
        first = input("Enter the first name:")
        if first == '':
            print("This field is required.  Please enter your first name.".format(first))
        elif first.isalpha():
            if len(first) < 2:
                print("{} is not a valid first name.  It is too short. Please enter a valid first name.".format(first))
        else:
            break

        
    #last name
    while True:
        last = (input("Enter the last name:"))
        if last == '':
            print("This field is required.  Please enter your last name.".format(last))
        elif len(last) <2:
            print("{} is not a valid last name.  It is too short. Please enter a valid last name.".format(last))
        else:
            break


    # zipcode
    while True:
        zip_code = (input("Enter the ZIP code: "))
        if zip_code.isdecimal() == False:
            print("The ZIP code must be numeric. Please enter a valid zip code.")
        else:
            break
    
        
    #employee ID:
    while True:
        employee_ID = (input("Enter an employee ID:")) 
        if not re.match('(^[A-Z]{2}-[0-9]{4})', employee_ID):
            print("{} is not a valid ID. Please enter a valide employee ID.".format(employee_ID))
        else:
            break
problem5_2
Reply
#2
try:
problem5_2()
last line.

without the parenthesis, you get the address of the object, with paren's, execute it
Reply
#3
sorry -the () dropped off when I was copying/pasting code. I don't see an edit button in my original message so I can add them.

In any case - I do have them in my code. Here's an example of what I see when I execute:
Quote:Enter the first name:
This field is required. Please enter your first name.
Enter the first name:k
k is not a valid first name. It is too short. Please enter a valid first name.
Enter the first name:kkkk
Enter the first name:kkkkkk
Reply
#4
When I added (), it worked, asking first, last ...
Reply
#5
Well that's odd. I tried it again and double checked I had the () and it's still keeps just asking me to enter first name. I'm using Jupyter - what are you using? I've tried restarting the kernel/cell and re-running. Same results.
Reply
#6
It should work. I have tried the scenario in IPython. It works as expected. Run the script in a terminal window to test it.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
Your else: break needs to be indented. If you enter a valid name, the elif/alpha triggers, and checks the length. But if the length is valid, it doesn't go to the else, because the else is associated with the elif/alpha, not the if/len.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020