Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Number Game App Squared

Didier Borel
Didier Borel
2,837 Points

squared function

i don't understand why this is not working

squared.py
def squared(arguement):
    try:
        int(arguement)**2
    # If it can do that it will go here
    except ValueError:
        arguement*len(arguement)

    # If it cant it will go here.

4 Answers

Its all great, but you are suppose to return a value.

# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"

def squared(arguement):
    ret_value = 0
    try:
        ret_value = int(arguement)**2
    # If it can do that it will go here
    except ValueError:
        ret_value = arguement*len(arguement)
    return ret_value
Didier Borel
Didier Borel
2,837 Points

Hi krishna, thxs a lot for your answer it was helpful, and gave me the small step i was missing. When I wrote your code in the challenge, i got a correct answer. when i write the code workspaces I get no answer. I just get backed the name of the file, (here I named it python squared.py), so 2 questions:

  1. why is that? simply because I never gave an arguement?
  2. so I wrote this in workspaces, and got back this answer def squared(arguement): arguement=4 ret_value = 0 try: ret_value = int(arguement)**2 # If it can do that it will go here except ValueError: ret_value = arguement*len(arguement) return ret_value

answer treehouse:~/workspace$ python squared.py
File "squared.py", line 9
return ret_value
^
IndentationError: unindent does not match any outer indentation level

can you explain this to me?

So this is how compiler generates the error and points where it got the error.

answer treehouse:~/workspace$ python squared.py
File "squared.py", line 9
return ret_value
^
IndentationError: unindent does not match any outer indentation level

First line: You ran the code with python as compiler.

Second line: Error occurred while compiler was going through line 9

Third line tells: Error occurred at which point exactly in the line.(here the upper arrow shows at very first token)

Fourth line tells: What the problem was with the code. Here it says, it indentation.

My guess, you might have missed the indentation(or less number of spaces) before the return statement.

Solution, is simple but nothing straight forward. Since indentation specifies the block in python you have to take care of indentation.

To solve this problem, go through the code, null/remove all the indentation and again specify the indentation.

In my experience this generally happen when indentation space varies. Tab/indentation length is 2-spaces, 3-spaces or 4-spaces sometimes. There is nothing particular, just maintain a uniform indentation. I use 4-space indentation but many people uses 3-space also.

Didier Borel
Didier Borel
2,837 Points

Thanks for this very complete and helpful answer, Krishna! I will have a look at the indentation issue. another question: in fact it is question number one above : when i write your code workspaces I get no answer. I just get backed the name of the file, (here I named it python squared.py),

why is that? simply because I never gave an arguement?

When you run a python file,

python <file-name>.py

you will not get anything unless there is some output being sent from the file. When running using compiler(unlike in IDLE) you will not get anything on scree by simply stating the variable. e.g.

In IDLE:

>>> a = "Hello World!!!"
>>> a
Hello World!!!

You see above is a output


In File using compiler

a = "Hello World!!!"
a

While running this code in Terminal/Command_prompt

$ python <filename>.py
$

There is no output.


But if you change the code to...

In File using compiler

a = "Hello World!!!"
print(a)

While running this code in Terminal/Command_prompt

$ python <filename>.py
Hello World!!!
$

There is output.

Also, just defining the function wont do anything, you'll have to call it as well. For e.g.

def printHello():
    print("Hello")

.

$ python <filename>.py
$ 

There is no output

.

But

def printHello():
    print("Hello")

printHello()

.

$ python <filename>.py
$ 

There is output

Defining a function is just like declaring a variable. You actually have to call that functionality.

So you have to call the function you declared.

If it accepts an argument and you call without an argument it'll give an error, unless you have provided a default value to those arguments.

Didier Borel
Didier Borel
2,837 Points

great- thanks so much Krishna. so now how do I run something in idle? where do i load that?

In Terminal/Command_prompt, type python

$ python

and you'll see... .

Python 3.6.5 (v, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>>