Ok so me and my friend are doing a weekend challenge... Whoever makes the best game wins.
Im trying to make a game like farmville but its called mineville and its just a little different. My problem is that theres this part in the code that keeps repeating, and i can't get it to stop. What should i do? Please keep it simple as i am just a beginner. Here's the code.

scores = []
choice = None

while choice != "0":

    print(
        """
Welcome to Main Menu! Select what you would like to do!

0 - Exit
1 - Go to the Mine
2 - Go to the Shop
3 - Display Inventory
        """
        )

choice = input("Choice: ")
print(choice)

It might look familiar to some of you because i got it from my 'Python for the Absolute Beginner' book. (Don't worry, the challenge allowed the use of our programming books. Lol) Any help would be greatly appreciated! Thanks in advance.

Dani AI

Generated

Good catch by and — the menu keeps printing because the statement that reads and updates the choice value is not inside the while loop, so the loop condition never changes. Moving the input/prompt and any handling for the choice into the loop fixes the logic and lets the loop actually terminate when the exit option is chosen.

A common, robust pattern is to print the menu, read the choice, then branch (or break) based on that choice. For example:

menu = ("Welcome to Main Menu!\n\n"
        "0 - Exit\n"
        "1 - Go to the Mine\n"
        "2 - Go to the Shop\n"
        "3 - Display Inventory\n")

while True:
    print(menu)
    choice = input("Choice: ").strip()   # use raw_input() on Python 2
    if choice == "0":
        break
    elif choice == "1":
        go_to_mine()
    elif choice == "2":
        go_to_shop()
    elif choice == "3":
        show_inventory()
    else:
        print("Invalid option, try again.")

Troubleshooting tips: keep indentation consistent (use 4 spaces, do not mix tabs and spaces), put any debug prints inside the loop to see values changing, and use .strip() to ignore accidental whitespace. If the program still seems stuck, interrupt with Ctrl+C in the terminal and verify the input line is reached each loop iteration. This addresses the infinite-print behavior that and identified and gives a clearer structure for adding the Mine/Shop/Inventory logic.

Recommended Answers

All 3 Replies

Indent the last 2 lines to put them in the while's body.

You have only print is indented inside while.

Thanks bro!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.