sleepylog sleepylog - 1 year ago 42
Python Question

Unexpected error message returned to user

The main menu options on the following code run fine, until an 'invalid entry' is made in either choice 1 or 2, even if the entry is correct. What happens next, is when choosing option 3 or 4 to display either file, then the 'invalid entry' error message from the name registration process, is displayed to the user, even though the user isn't in that menu option.

It seems like the script is looping somewhere but I can't figure out where. As I said, it only happens if that initial 'invalid entry' message is invoked in either option 1 or 2. If it isn't invoked, I can keep registering names and displaying files without problems.

Any help will be greatly appreciated.

import sys


def main():

main_menu_choice = input("""Please choose from the following
1. Register a child.
2. Register a staff member.
3. Children.
4. Staff.
5. Exit.""")

if main_menu_choice == "1":
child_first_name = input("Enter the child's first name")
# check that no empty string or digit is entered
while child_first_name == "" or any(str.isdigit(c) for c in child_first_name):
print("Invalid entry.")
main()

else:
child_last_name = input("Enter the child's last name")

while child_last_name == "" or any(str.isdigit(c) for c in child_last_name):
print("Invalid entry.")
main()

child_name = (child_first_name + " " + child_last_name).title()

file = open("Children.txt", 'a')
file.write(child_name + "\n")
file.close()
main()

elif main_menu_choice == "2":
staff_first_name = input("Enter the staff member's first name")

while staff_first_name == "" or any(str.isdigit(c) for c in staff_first_name):
print("Sorry I didn't catch that\nPlease start again.")
main()

else:
staff_last_name = input("Enter the staff member's last name")

while staff_last_name == "" or any(str.isdigit(c) for c in staff_last_name):
print("Sorry I didn't catch that\nPlease start again.")
main()

staff_name = (staff_first_name + " " + staff_last_name).title()

file = open("Staff.txt", 'a')
file.write(staff_name + "\n")
file.close()
main()

elif main_menu_choice == "3":
file = open("Children.txt", 'r')
lineList = file.readlines()
lineList.sort()
print("List of children's names in alphabetical order:")
for line in lineList:
print(line, end="")
file.close()

elif main_menu_choice == "4":
file = open("Staff.txt", 'r')
lineList = file.readlines()
lineList.sort()
print("List of staff member's names in alphabetical order:")
for line in lineList:
print(line, end="")
file.close()

elif main_menu_choice == "5":
print("Exit.")
sys.exit(0)

else: #If an invalid choice was made from main_menu_choice
print("Invalid Command, try again.")
main()
main()

Answer Source

I think your problem lies in:

while child_first_name == "" or any(str.isdigit(c) for c in child_first_name):
print("Invalid entry.") 
main()

You are using while in this condition check. This makes it keep on looping because the condition remains true. Replace this condition check using an if. Hope this helps.

EDIT: To terminate the program at some point, you need to use return. You can add this after calling main() to avoid the rest of the code from being executed. Try to avoid recursive calls when it's not necessary. This piece of code can be done using loops.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download