Stumped - Unexpected EOF while parsing - HUH?


Hi everyone.
I was stumped by an exercise in functions. Any help would be appreciated.

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
Oops, try again. It looks like you have an error in your code. Check the error message for more info! - unexpected EOF while parsing (, line 0)

<What do you expect to happen instead?>
I expected the program to ask for input and return an absolute value if the input is an int or float and return “Nope” otherwise.

Thanks in advance for the help!


def distance_from_zero(num):
num = input("Type in a number: ")
if type(num) == int or float:
return abs(num)
return “Nope”

<do not remove the three backticks above>

@jrebollo: Hello, Jrebollo!
The problem with your code is that you’re comparing two things: the type of the input from the user and the type of value float, such will cause the condition to be True and so your program will try to get the absolute value from the input from the user.

Whenever we use input or raw_input, we are bringing back a string value and storing such into a variable, that’s the usual thing to do, why would we ask the user to input something and not store it, right?

Right after that line, where you get the input, you use the function type and try to find out what type is your input. If you do it the way it is will be a string and such is not equal to int but since the logical operator we are using is OR, it means we need at least one of our statements to be True so our condition will be evaluated as True and the execution of my program will execute the lines of code inside of it.
In your case you’re comparing something that is far from being an integer to something that will always be float, you’re trying to compare the type of input with another type of value and such will always be evaluated as True.

After all of this, you’ll tell your compiler to bring back the absolute value of your input and since it’s a string, the error will be caused!

Try to fix that by asking if the type of num is also a float, same way you did for integer checking.

Hope it helps!

Note: when I executed your code, I got the following error message: “TypeError: bad operand type for abs(): ‘str’”. Are you sure that’s what you got from it ?

1 Like

Hi g4be,

Thanks for responding.
The program did not execute at all.
I was not able to enter anything and the output screen simply said “NONE”
I also got a different error than you did.
The error I got is listed in my first post.
Any thoughts?


I found something I didn’t know about it, input and raw_input are two different cookies in the Python cookie bag (living and learning :heart:)

[quote]If you use input, then the data you type is is interpreted as a Python Expression which means that you end up with gawd knows what type of object in your target variable, and a heck of a wide range of exceptions that can be generated. So you should NOT use input unless you’re putting something in for temporary testing, to be used only by someone who knows a bit about Python expressions.

raw_input always returns a string because, heck, that’s what you always type in … but then you can easily convert it to the specific type you want, and catch the specific exceptions that may occur. Hopefully with that explanation, it’s a no-brainer to know which you should use.[/quote]
From here - StackOverflow, ref here

Try changing from input to raw_input, it should work!
If not, let us know :slight_smile:

I changed from input to raw_input.
Here’s the error I got: Oops, try again. Your function seems to fail on input -10 when it returned ‘Nope’ instead of ‘10’

I didn’t input anything.

Oh well, that’s weird, it should’ve worked.
Try executing your code on and see if the error continues.
If you’ve fixed what I told you, and also you’re asking for the value before actually calling your function, it should work.

If not, post the code here and we’ll take a look!

Here’s a copy of my code:

num = raw_input("Type in a number: ")
if type(num) == int:
return abs(num)
elif type(num) == float:
return abs(num)
return “Nope”

I am not able to access the link you posted.
Please let me know your thoughts.

@jrebollo: Remove that raw_input inside of your function.
You’re supposed to first get the value to later call the function.

By doing the way it is in your current code, regardless of your input, you’ll ask for the user to once again input the value and such will cause the error because raw_input returns a string,not an integer.

I’ve changed it back to input and called the function.
It only asks for input then errors out again no matter what input I give it.
This is odd.

@jrebollo: Did you put def right before your function header?
Also, regardless of your input, if you do not convert it and store it on another, or the same, variable, it will continue to be a string.

variableTest = raw_input("Inform a number:") print type(variableTest) #str variableTest = int(variableTest) print type(variableTest) #int
Q: Can I try to convert it?
A: Yes but the way it is, I wouldn’t recommend. Since you have no source of exception handling, it would stop your code.

For now, consider the input to be anything, string or numeric, so it will work.

Hi g4be,

I tried using print instead of return and was able to make the program work.
After the program executes successfully, it errors out.
At least I’m getting closer.