If condition fails to evaluate input from str(input()).upper() + SyntaxError: unexpected EOF while parsing (<string>, line 0)


Hi! I'm making a little text adventure in the console, and i figured that instead of writing in the restart code at the end of every statement, i'd just make a function and paste it at the end.

The lesson i'm making it in: https://www.codecademy.com/en/courses/python-beginner-BxUFN/4/1?curriculum_id=4f89dab3d788890003000096

However, i can't get the conditions of the if statements in try_again() to evaluate the strings from tryagain without typing in the quotes in front and behind it into the console input, and when i do type in 'n' and the program stops like intended with print "Thanks for playing!" running at the end, i get his error message:

Oops, try again. The following exception was raised when calling the_flying_circus(): SyntaxError: unexpected EOF while parsing (, line 0)

I don't get any errors in the console. Anyway, here's the code. I hope i was specific enough.

def try_again():
    tryagain = input("Wanna try again? \nY or N :").upper()
    if tryagain == 'Y':
    elif tryagain == 'N':
        print "Thanks for playing!"
        return True
        print "Y or N, buddy"

def the_flying_circus():
    num = int(input("Welcome to the number freak 1.0! Type in a number between 0 and 10!"))
    if num <= 5:
        go_higher = int(input("Why, you're laying flat on the floor! Go higher!"))
        if go_higher < num:
            print "Well, let's just burry our heads in the sand then..."
        elif go_higher > num and go_higher < 10:
            print "Well, that is admittedly higher, but i was thinking more adventurous.."
        elif go_higher > 10:
            print "Now we're talking!"
            print "Buddy, just stick to the numbers"
    elif num >= 6 and num < 10:
        print "That's definitely on the high side! Could do better, though."
    elif num > 10:
        print "A rebel off the batt, eh? I like it!"
        print "A number would do just fine"


If you change the behaviour of your function away from what the exercise describes and expects, then yeah, it'll complain, no surprises there.

You're also better off taking input as strings instead of treating the user input as python code.


Would you mind explaining a bit more?


You would have to explain what you're missing, I won't just do "more"


...Why does my python code not interpret the input as strings, but as "Python code", that by what you mean is?


>>> print input.__doc__
input([prompt]) -> value

Equivalent to eval(raw_input(prompt)).
>>> print eval.__doc__
eval(source[, globals[, locals]]) -> value

Evaluate the source in the context of globals and locals.
The source may be a string representing a Python expression
or a code object as returned by compile().
The globals must be a dictionary and locals can be any mapping,
defaulting to the current globals and locals.
If only globals is given, locals defaults to it.


Right, so now as to how this applies to my code. Or is this the best i can expect?
Thing's i don't know what is:

Locals, globals, returned by compile(), must be dictionary, any mapping.


If you gave me access to your program, then you would be giving me control over the user that the program is running as. Files, network, cpu. Whatever rights that program has, I would control.

>>> input()
globals()['__builtins__'].open('fileonmysystem', 'r').read()
'this is\na file on my\nsystem\n'

I could just as well delete that file, or add things to some startup file to run every time the user logs in. I could start a loop that asks for input and then enter as many commands as I felt like.


Point to where i give that access in my code. You really gotta tone it down.


input evaluates text like python code. You should just read text, not execute it.
If your program reads a command to delete all files, then it shouldn't be doing that.


So basically don't use input. Thank's for a whole lot of convoluted answers, i'll go research why myself.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.