简体   繁体   中英

looping back to a main menu in python

I am making a texted based game on python 3.3. this is the start to my code:

while True:

  print ("You must answer all questions in block capitals")
  print ("Welcome to maze runner")
  print ("To learn about the controls press C")
  print ("To learn the about the different types T")
  print ("To play press START")
  run = input ()
  #Controls
  if run == "C":
     print ("To attack press H")
     print ("To walk forward press W")
     print ("To turn left press A")
     print ("To turn right press D")
     print ("To turn around press S")
     print ("To block press B")
     print ("To open stats press Q")
     print ("To open this screen press C")
     print ("To close this screen press C")
  #Types
  if run == "T":
    print ("Soldier. Press S for more info")
    print ("Archer. Press A for more info")
    print ("Mage. Press M for more info")
    print ("Changeling. Press C for more info")
    print ("Rouge. Press R for more info")
    print ("Press M to go to main menu")
    type_info = input()
    if type_info == "M":
       break
    #Solider info
    if type_info == "S":
       print ("Soldier: Good at close combat")
       print ("         Weak at long range combat")
       print ("         Average speed")
       print ("         Average blocker")
       print ("Press M to go to main menu")
       return_to_menu = input()
       if return_to_menu == "M":
          break      
    #Archer info
    if type_info == "A":
       print ("Archer: Weak at close combat")
       print ("        Good at long range combat")
       print ("        Good speed")
       print ("        Average blocker")
       print ("Press M to go to main menu")
    #Mage info
    if type_info == "M":
       print ("Mage: Weak at close combat")
 print ("      Good at long range combat")
       print ("      Average speed")
 print ("      Good blocker")
       print ("Press M to go to main menu")
    #Changeling info
    if type_info == "C":
       print ("Changling: Average at close combat")
       print ("           Average at long range combat")
       print ("           Average speed")
       print ("           Average blocker")
       print ("Press M to go to main menu")
    #Rouge info
    if type_info == "R":
       print ("Rouge:  Average at close combat")
       print ("        Average at long range combat")
       print ("        Good speed")
       print ("        Good blocker")
       print ("Press M to go to main menu")

I added the code from below to some of the code. Know when I run it I get an error saying inconstant use of space of tabs and highlight the end of the line saying:

  print ("Mage: Weak at close combat")

why is there are not space after the line

When I run this, I get:

...        print ("Mage: Weak at close combat")
...  print ("      Good at long range combat")
  File "<stdin>", line 51
    print ("      Good at long range combat")
                                            ^
IndentationError: unindent does not match any outer indentation level

Check the "Good at long range combat" line - it appears to have a tab instead of spaces.

You could put all of the above code inside a while True: loop, and break out of the loop when you don't want to return to the main menu.

while True:
    # your code here
    return_to_menu = input()
    if return_to_menu != 'M':
        break

On a side note, I'd perhaps use else if s. They are a tad faster because when one of the conditions matches, the others in the if ... else if block are not evaluated.

print ("Soldier. Press S for more info")
print ("Archer. Press A for more info")
print ("Mage. Press M for more info")
print ("Changeling. Press C for more info")
print ("Rouge. Press R for more info")
print ("Press M to go to main menu")

So M is both for Mage and Menu. Whoops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM