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


#1

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':
        the_flying_circus()
    elif tryagain == 'N':
        print "Thanks for playing!"
        return True
    else:
        print "Y or N, buddy"
        try_again()

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..."
            try_again()
        elif go_higher > num and go_higher < 10:
            print "Well, that is admittedly higher, but i was thinking more adventurous.."
            try_again()
        elif go_higher > 10:
            print "Now we're talking!"
            try_again()
        else:
            print "Buddy, just stick to the numbers"
            try_again() 
    elif num >= 6 and num < 10:
        print "That's definitely on the high side! Could do better, though."
        try_again()
    elif num > 10:
        print "A rebel off the batt, eh? I like it!"
    else:
        print "A number would do just fine"
        try_again()
        
the_flying_circus()


#2

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.


#3

Would you mind explaining a bit more?


#4

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


#5

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


#6

>>> 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.

#7

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.


#8

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.


#9

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


#10

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.


#11

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


#12

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