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 Functions and Looping Create a Function

Ali Zalzale
Ali Zalzale
901 Points

i can't do this help

squaring.py
def square(number):
    Return (square)*2
number = (input("choose a number"  ))
square = (number)**2
print (square)

1 Answer

Owen Bell
Owen Bell
8,060 Points

input returns a string based on the user input. You can't apply a power raising operation to a string, so we first need to convert the data type to an integer value using the int() function. The example below works for when the user enters an integer: you could build it out further to catch floating points for int conversion by using float() before, or within int().

The function itself is currently multiplying its parameter number by 2 instead of squaring it. As you've done outside the function body, just add one more * to the expression in the function body.

Then, you're not actually calling the function - you're just hard coding what the function should do if it were to be called. Call the function, passing in number as an argument, and save this to a new variable. Instead of square, I called it number_squared below just to avoid confusion between global variable square and function square().

All that sums up to:

def square(num):
  return num ** 2

number = int(input("Choose a number:  "))
number_squared = square(number)
print(number_squared)