For a school project me and my group partner made a code, I tested each function in a separate test file to see if they worked and it all looks good, but the menu just isn't working as intended. My brain can't seem to figure out where I went wrong, I would love a second opinion on this.. thank you so much <3 Here is my code!
def mainmenu():
print("Hello! Welcome to The Sprint Project Company! Please choose from 1-5: ")
print("1. Simple IPO Program")
print("2. IFs and Loop sample")
print("3. Strings and dates")
print("4. Data files and Default Values")
print("5. Quit")
while True:
choice = input("Enter choice (1-5): ")
if choice == 1:
ipo()
elif choice == 2:
ifloop()
elif choice == 3:
stringsdates()
elif choice == 4:
datafiles()
else:
break
mainmenu()
Whenever I run this it just automatically ends. I even tested by putting a print section under the else but it just skips straight to ending the code. Thank you so much for looking at my question <3
CodePudding user response:
There are two points on your code. First, the function "input()" returns a string, hence you are comparing a STRING with an INTEGER, then it evalueates to False. It is like you are comparing 1 with '1', and they are not the same.
Second, your function mainmenu() must be put inside the loop. Make this two changes and it will work.
while True:
mainmenu() # Add the function here.
choice = int(input("Enter choice (1-5): ")) # Change here
if choice == 1:
CodePudding user response:
You should be calling the mainmenu()
function before the loop.