The following are links to additional questions that our community has asked about this exercise:
This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!
Not seeing your question? It may still have been asked before – try () in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post ().
With the code in this exercise, I tried to make the script receive input from the console, but it returns ‘Unicode’ as the type. The function works fine if you pass the argument from code, but do I need to define/transform the raw_input to make it work?
Here is my code:
x = raw_input("Enter a number: ")
print type(1)
print type(1.0)
print type(x)
print distance_from_zero(x)
def distance_from_zero(x):
if type(x) == type(1) or type(x) == type(1.0):
return abs(x)
else:
return “Nope”
I’m trying to do something similar, and I think the problem is that raw_input() always outputs a string.
I tried using x = int(rawinput("Enter a number: ") to transform the input to an integer, which works if the user types an integer, but fails if they type a float or a string.
Not really, just adding information to @stephenmak5974281141 question.
I assume that if you want to use raw_input() for this exercise, you need to check the input somehow before accepting it to the function?
The instructions: " First, def a function called distance_from_zero , with one argument (choose any argument name you like)."
So I “choose any argument name [I] like”: “distance_from_zero(5)” and get the error. I look at the solution and it says “distance_from_zero(num)” and wont let me do anything but that: “num”
Change the instructions so that the answer is accepted, please. Spending time to get it right when it’s already correct is so exhausting as this is a constant problem throughout all these exercises. I always want to figure out my mistake first just to see there isn’t one. There’s just a specific way you want the answer yet didn’t say that or it’s just not reading it properly. I can’t take pics but I’ve had the exact answer as the Solution but it kept not being accepted.
You got an error because you cannot assign an integer as a parameter in
the function.
In python, a paramater is variable, and should follow the rules for a valid variable name.
Rules for Python variables:
A variable name must start with a letter or the underscore character.
A variable name
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
You cannot assign the integer 5 as a parameter because a variable name can’t begin with a digit.
So, try naming the parameter to ‘anything argument name you like’ EXCEPT integers and keywords.
Hello. The answer to this question might be quite technical, but I’m still curious. I hope someone can give me an answer.
In the course of debugging my code, I had written print(type(dis)) at the beginning of my function, like so:
def distance_from_zero(dis):
print(type(dis))
if type(dis) == int or type(dis) == float:
return abs(dis)
else:
return "Nope"
The output to the console was a long list of varying types, including some things we haven’t learned at this point, such at “dict” and “tuple”. What’s up with that? What’s going on with the dis variable in the course of this program?