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 squaring function

I am having trouble with the syntax of writing a function

def squared(num):
  num = int(input("enter number "))
  print(num*num)

squared(num)

I tried running this on the console of workspaces and I was told that "num is not defined" I realized it was only defined inside of the function and maybe that is why I was getting error. how do I fix this

1 Answer

You have 2 problems here. Yes, you're right about num not being defined when you call it. It's a function parameter, so it's only available in that function. When you call a function, you need to pass it an actual value (or a variable that contains one), so a number literal would work best here. Second of all, you probably shouldn't call input. Judging by the context, it seems like you should be operating on num directly instead of calling input and reassigning num

Thank you. That makes sense