Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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)