Python Python Basics Functions and Looping Create a Function

Matthew Earlywine
Matthew Earlywine
3,406 Points

Trouble with Task 2

Great now that you have created your new square method, let's put it to use.

Under the function definition, call your new function and pass it the argument 3.

Since your square function returns a value, create a new variable named result to store the value from the function call.

squaring.py
def square(argument):
    try:
        return int(argument) ** 2
    except ValueError:
        return str(argument) * len(argument)
Matthew Earlywine
Matthew Earlywine
3,406 Points

I am not sure what to do for this one.

4 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

I am asking you to use the function you just created.

The function is defined correctly, and I want you to call it with the value of 3. Since it returns a value, store it in a variable named result.

That make sense?

Keith McDonald
Keith McDonald
816 Points

Craig, Ive written out the task and performed it correctly but when I go to run the square function it just spits out a syntax error near unexpected token. What am I doing wrong with this?

Keith McDonald
Keith McDonald
816 Points
def square(number):
       return((number)**2)

result = square(3)
Craig Dennis
Craig Dennis
Treehouse Teacher

Hmm...wonder if it's those parentheses around that return statement. Try removing those. Return is a statement, not a function so you don't need to call it. Also the parents around number are not needed.

If you use the preview pane it'll tell you were the error is.

I'm away from a computer right now, but let me know if you are still stuck.

Keith McDonald
Keith McDonald
816 Points

yeah so ive changed the code to

def square(number):
     return number**2

Result = square(3)

I ran it in treehouse:~/workspace$ python number.py and it returns back to treehouse:~/workspace$

I put it into the REPL as well and it just says invalid syntax line 1 python number.py ^ Im not sure what Im missing. Im guessing its something simple.

Craig Dennis
Craig Dennis
Treehouse Teacher

If you lowercase that Result to result it passes for me.

Is it not working for you on the challenge?

Keith McDonald
Keith McDonald
816 Points

Thank you sir that seems to have done the trick.

Antonio De Rose
Antonio De Rose
18,677 Points

it is asking to call the function, that you created and pass the argument 3, and then store the value in a variable called result.

3 thins you will have to know.

1) how do you call the function you created ? square

2)how to pass an argument into the call function ? square(3)

3) how to store the value from the result you get ? result = square(3)

Matthew Earlywine
Matthew Earlywine
3,406 Points

This is what I came up with but it's not passing. def square(num): try: int(num) return(num * num) except ValueError: return(num * len(num))

square(num = num3) num3 = result

Pierre ILYAMUKURU
PRO
Pierre ILYAMUKURU
Pro Student 1,119 Points

Hello, I did my code and pass the first task and the second task failed. Can you please help me to figure out where I am doing the mistake. Here are my code

def square(number):
    value=(number*number)
    return(value)
    result=square(3)
    print(result)
Antonio De Rose
Antonio De Rose
18,677 Points

@Pierre ILYAMUKURU

I ran your code, and it did say, success, I am sure, that you have an issue to do with the indentation. next time, when you post your code, please the code formatting option, under markdown sheet.

def square(number): 
    value=(number*number) 
    return(value) 

result=square(3) 
print(result)
Pierre ILYAMUKURU
Pierre ILYAMUKURU
Pro Student 1,119 Points

Thank you so much, Antonio is working perfectly right now.