Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Cant return to a previous menu in python

+5 votes
asked Nov 7 by Nathaniel Rubas (170 points)

Here is the code so far (things dealing with menus being activated are highlighted)

SectionSel=True
while SectionSel:
    print ("""\n---------
1.Fight
2.Shop
3.Explore
4.Quit
---------"""

SectionSel=input("What would you like to do?: ") 
    if SectionSel == "1":
        SectionSel=False
        Fight=True
        hp = 100
        ehp = 20
        hpchance = 3
    elif SectionSel == "2":
        print("e")
    elif SectionSel == "3":
      print("e")
    elif SectionSel == "4":
      print("e")
    elif SectionSel == "":
      print("\n Not Valid Choice Try again")
while Fight:
    print('\nEnemy HP', ehp)
    print('Your HP', hp)
    print("""--------
1.Attack
2.Heal
--------""")
    Fight=input("Your move: \n") 
    if Fight == "1": 
      dmg = random.randint(1, 20)
      ehp = ehp - dmg
      dmg = random.randint(1, 20)
      hp = hp - dmg
      if hp < 1:
        print("\nYou Died")
        break
      elif ehp < 1:
        print("\nVictory!")
        SectionSel=True
        Fight=False
    elif Fight == "2":
      if hpchance < 1:
        print("\nCannot Heal!\n")
      elif hpchance > 0:
        hpchance = hpchance - 1
        hp = hp + random.randint(1, 25)
        hp = hp - random.randint(1, 10)
        if hp < 1:
            print("\nYou Died")
            break
        elif ehp < 1:
            print("\nVictory!")

        elif Fight == "":
            print("\n Not Valid Choice Try again")
            SectionSel=True
            Fight=False

Basically, the goal is to go back to SectionSel once the fight finishes. However, the program just breaks. How do I get this back working? It worked at one point I believe, but now I don't know what's been done wrong.

1 Answer

+1 vote
answered Nov 9 by Peter Minarik (63,320 points)

There was a missing closing parenthesis at the end of your first print() and a missing indentation from your first input().

After this, your code ran, and seemed to work OK to me. Here's the input/output:

---------
1.Fight
2.Shop
3.Explore
4.Quit
---------
What would you like to do?: 1

Enemy HP 20
Your HP 100
--------
1.Attack
2.Heal
--------
Your move: 
1

Enemy HP 8
Your HP 82
--------
1.Attack
2.Heal
--------
Your move: 
1

Victory!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...