Stumped - Unexpected EOF while parsing - HUH?


#1



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


https://www.codecademy.com/courses/python-beginner-c7VZg/4/3?curriculum_id=4f89dab3d788890003000096#


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)


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)
    else: 
        return "Nope"


#2

@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 ?


#3

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?

jrebollo


#4

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:)

From here - StackOverflow, ref here

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


#5

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.


#6

Oh well, that's weird, it should've worked.
Try executing your code on repl.it 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!


#7

Here's a copy of my code:

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

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


#8

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


#9

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.


#10

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


#11

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.

jrebollo