Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k points)

I need to find the best way to type a quit function for menu-driven programming so that the quit terminates the code only in one reaction

Have a look at my code, edit if possible:

print("\nMenu\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit")

choose=input(">>> ")

choice=choose.lower()

while choice!="q":

    if choice=="v":

        highScore()

        main()

    elif choice=="s":

        setLimit()

        main()

    elif choice=="p":

        game()

        main()

    else:

        print("Invalid choice, please choose again")

        print("\n")

print("Thank you for playing,",name,end="")

print(".")

1 Answer

0 votes
by (26.4k points)

Try to  insert menu and parsing in a loop. In case, if a user need to quit, use "break" to break out of the loop.

Code:

name = 'Studboy'

while True:

    print("\nMenu\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit")

    choice = raw_input(">>> ").lower().rstrip()

    if choice=="q":

        break

    elif choice=="v":

        highScore()

    elif choice=="s":

        setLimit()

    elif choice=="p":

        game()

    else:

        print("Invalid choice, please choose again\n")

print("Thank you for playing,",name)

print(".")

Want to learn python? Come, Join the python online course fast to gain more knowledge in python.

Browse Categories

...