3

Im new to python and taking a class. Below's my code. My problem is at the very last line I want it so that if someone simply presses enter it will count as if someone made an input other than (1/2/3 or 4) which would basically reply with the last elif and restart the question. But at the moment it gives me this:

SyntaxError: unexpected EOF while parsing. 

Can anyone help?

def menu_loop():
    ans = True
    while ans:
        print("""
        1. My freedom awaits me, give me my blade!
        2. I've never fought before, might you help me?
        3. I have already forsaken freedom, I will fight till the death.
        4. Fighting is for the wicked. I refuse to fight.

        Press enter to quit
        """)

        ans= input("What would you like to do now?")
        if ans == 1:
            main_loop()
            break
        elif ans == 2:
            instructions_loop()
            break
        elif ans == 3:
            gauntlet_loop()
            break
        elif ans == 4:
            print("\nMen... come relieve this man of his life.")
            time.sleep(2)
            end_game_loop()
            break
        elif ans == '': 
            print("\nDo you not speak Latin, slave?")

Thank you, I found the answer thanks to Christian. I guess the site I got instructions from was on Python 3 and I'm on 2 (it's for a university assignment). It's working now, thanks lots!

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
GOAT
  • 33
  • 4

3 Answers3

0

if you are using python 2 then change:

ans = input("What would you like to do now?")

to:

ans = raw_input("What would you like to do now?")  

raw_input returns a string while input returns an integer this was changed in python 3, in 3 there is only input and it returns a string

so if you are using python 2 change it to raw_input then call your other conditions like this:

if int(ans) == 1:
    main_loop()
    break
Serial
  • 7,925
  • 13
  • 52
  • 71
  • Thank you, this helped quite a bit. I changed the input to raw_input because I am indeed using python 2(.7). When I added the int part though, the thing broke so instead I just put the numbers inside quotation marks and everything is working smooth now. Very much appreciated :D – GOAT Sep 29 '13 at 22:44
  • yes you can do that too, Im glad i could help :) good luck with the rest of your project! – Serial Sep 29 '13 at 23:25
0

Change the last elif condition to elif not ans

def menu_loop():
    ans = True
    while ans:
        print("""
        1. My freedom awaits me, give me my blade!
        2. I've never fought before, might you help me?
        3. I have already forsaken freedom, I will fight till the death.
        4. Fighting is for the wicked. I refuse to fight.

        Press enter to quit
        """)

        ans = input("What would you like to do now?")
        if ans == 1:
            main_loop()
            break
        elif ans == 2:
            instructions_loop()
            break
        elif ans == 3:
            gauntlet_loop()
            break
        elif ans == 4:
            print("\nMen... come relieve this man of his life.")
            time.sleep(2)
            end_game_loop()
            break
        elif not ans: 
            print("\nDo you not speak Latin, slave?")

Edit: The answer is for python 3. For python 2.7, use raw_input instead of input, and accordingly use int(ans) or string literals.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
0

use raw_input instead of input. input will try to evaluate the input, evaluating an empty string raises an error. raw_input doesn't evaluate, just gives you a string. See this explanation.

def main_loop():
    pass
def instructions_loop():
    pass
def gauntlet_loop():
    pass
def end_game_loop():
    pass

next_loop_func = {
    '1': main_loop, 
    '2': instructions_loop, 
    '3': gauntlet_loop,
    '4': end_game_loop,
}
def menu_loop():
    while True:
        print("""
        1. My freedom awaits me, give me my blade!
        2. I've never fought before, might you help me?
        3. I have already forsaken freedom, I will fight till the death.
        4. Fighting is for the wicked. I refuse to fight.

        Press enter to quit
        """)

        ans = raw_input("What would you like to do now?")
        if ans in next_loop_func:
            next_loop_func[ans]
        else: 
            print("\nDo you not speak Latin, slave?")


menu_loop()
Community
  • 1
  • 1
Graeme Stuart
  • 5,837
  • 2
  • 26
  • 46