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

Amit Rathod
PLUS
Amit Rathod
Courses Plus Student 694 Points

I don't get the question

Can the question be more clear please? Not sure what's exactly the outcome does this question want?

squared.py
# EXAMPLES
# squared(5) would return 25
def squared(arg):

# squared("2") would return 4
# squared("tim") would return "timtimtim"

4 Answers

Dylan Davenport
PLUS
Dylan Davenport
Courses Plus Student 2,645 Points

It seems to be asking for a function that takes input and squares it.

For example: def squared(arg): return arg ** 2

Then to call the function you would type: squared(4) the output would be 16 which is 4 squared. Hope this helps.

Amit Rathod
PLUS
Amit Rathod
Courses Plus Student 694 Points

Thanks Dylan for replying. I get what you implying here but the question seems to be a confusion to me. Below is the question:


This challenge is similar to an earlier one. Remember, though, I want you to practice! You'll probably want to use try and except on this one. You might have to not use the else block, though. Write a function named squared that takes a single argument. If the argument can be converted into an integer, convert it and return the square of the number (num ** 2 or num * num). If the argument cannot be turned into an integer (maybe it's a string of non-numbers?), return the argument multiplied by its length. Look in the file for examples.

EXAMPLES

squared(5) would return 25

def squared(arg):

squared("2") would return 4

squared("tim") would return "timtimtim"


Is it just me or this question looks weird? Shouldn't it be more straightforward? and I still don't understand what this question wants.

Dylan Davenport
PLUS
Dylan Davenport
Courses Plus Student 2,645 Points

Oh ok I see what you're saying. It wants you to write a function that takes one argument. That argument can be an integer and if it is an integer it will return that integer squared. You can also input an integer as a string and convert it into an integer. If the argument passed is a string of non numbers (letters, etc.) it would return the length of the string (number of characters in the string) multiplied by the number of arguments. In this case there will only be on argument. So you would have to use if statements and the len() function to get the results you need. You can also use try and except statements to deal with error handling. I hope i explained it properly. Im a beginner myself haha

Amit Rathod
PLUS
Amit Rathod
Courses Plus Student 694 Points

Could figure out below:

def squared(num): try: return int(num) ** 2 except ValueError: return num * len(num)

argv = squared("number") print(argv)