FAQ: Learn Python: Functions - Review: Built-In Functions

3 posts were split to a new topic: Why Does This Work With Numbers, But Not Strings?

Hi! I’m on Python (something pretty new for me).

On functions, I’m trying to figure out what “return” does. For example

def biggest_number(*args):
print max(args)
return max(args) -----------> here

What does it do?

Thanks!

have you checked with FAQ:

What does return do?

hi! I’m not sure why this doesn’t work, can anyone tell me?

thanks!

1 Like

Hi,

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”

print distance_from_zero(-6)
print distance_from_zero(-56.983)
print distance_from_zero(“Abc”)

1 Like

same problem :confused:

please see this topic:

How to ask good questions (and get good answers)

Asking better questions will get better response.

I guess it was fixed, because it now works two years later.

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.

Do you still have a question, or?

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?

or you could write a separate function which validates the input.

Still happening. What’s the fix?

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?

Thanks for any insight.

It must have been a bug, I can’t reproduce the result, unfortunately. Here is what the output looked like:
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘int’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘float’>
<type ‘bool’>
<type ‘bool’>
<type ‘bool’>
<type ‘bool’>
<type ‘str’>
<type ‘str’>
<type ‘dict’>
<type ‘dict’>
<type ‘tuple’>
<type ‘tuple’>
<type ‘list’>
<type ‘list’>

the exercises tests your code by calling the function. the print() within your function will also print all the calls Codecademy is executing in the background

1 Like

I am confused about why my codes didn’t work.

The instructions were:

First, def a function called distance_from_zero, with one argument (choose any argument name you like).
If the type of the argument is either int or float, the function should return the absolute value of the function input.
Otherwise, the function should return "Nope"

My answer was:
def distance_from_zero(claire_number):
if type(distance_from_zero) = int:
return abs(distance_from_zero)
elif type(distance_from_zero) = float:
return abs(distance_from_zero)
else:
return “Nope”

The correct answers should be:
def distance_from_zero(claire_number):
if type(distance_from_zero) == int:
return abs(distance_from_zero)
elif type(distance_from_zero) == float:
return abs(distance_from_zero)
else:
return “Nope”

My question:
I set the argument as “claire_number” instead of “num”, which I think should be fine?
I also put the conditions separately.
Are my codes answering the instructions and will they work?

Good day,

I’ve been working on the built in functions review for the last couple days now, trying every avenue to try to get a success on it and I’m stumped. My code is:

def distance_from_zero(num):
if type(num) == int or type(num) == float:
return abs(num)
else:
return “Nope”

I’m not seeing where the issue is and it’s just not making sense to me. The answer above is the last permutation of the code I’ve entered after trying numerous avenues.