Python Menu - Driven Programs


#1

I can’t seem to print to the console any of my answers. When i type a number 1-5, I keep getting 'This is not a valid selection, please type the correct number.

def main():
    language = 0

    intro()
    menu()

    lanaguage = eval(input("Please type 1-4 from the menu or press 5 to quit. "))

    if language == 1:
        englishLanguage()
    elif language == 2:
        italianLanguage()
    elif language == 3:
        spanishLanguage()
    elif language == 4:
        germanLanguage()
    elif language == 5:
        print("Good Bye! ")
    else: #validate selection
        print("That is not a valid selection, please type the correct number\n")
        main() #call the main and start the loop again
    while language == 5:
        break #exits out of the program
    else:
        replayMenu()
    

def intro():
    print("Welcome to my Language Translator Program!\n")
    

def menu():
    print("Select a language so I can translate for you and say 'Good Morning!'")
    print("1. English")
    print("2. Italian")
    print("3. Spanish")
    print("4. German")
    print("5. Quit\n")

def replayMenu():
    startover = ""
    startover = input("Would you like to start from the beginning, yes or no? ")
    while startover.lower() != "yes":
        print("Thanks for using our Translator Program! Bye! ")
        break
    else:
        main()

def englishLanguage():
    for i in range(10):
        print("Good morning!")

def italianLanguage():
    print("Buongiorno!")

def spanishLanguage():
    print("Buenos Dias!")

def germanLanguage():
    print("Guten morgen!")

main()

#2

This line (shouldn’t) be doing anything.

Because your intention seems to be to overwrite that, so there’s no purpose for it. Remove it.
However, it does do something, it prevents your program from crashing. So:

  • It should do nothing because it gets overwritten
  • It does something

conclusion: it isn’t overwritten. That’s the bug.


Also, please don’t use eval to convert to int. The user isn’t entering python code.

Rather than creating a while loop so that you can break so that you can do nothing… Do nothing. That’s weird.


#3

I’m sorry but I still can’t get it to run.


#4

make sure to have no typos in variable names. Also look at other good points made by ionatan.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.