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

Jiten Mistry
Jiten Mistry
4,698 Points

Multiplying string but length of it

Im struggling with the last part of this and would appreciate any help.

Kind regards

squared.py
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(num):
    try:
        square = num * num
        return square
    except ValueError:
        n_num = num * len(num)
        return n_num

2 Answers

Close! Try converting "num" into an int before multiplying them.

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

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

Hope it helps and happy coding! ~Alex

Hi Jiten

The task requires you to convert the argument to an integer in the try block. And if that fails return num * its length.

see below

def squared(num):
    try:
        num = int(num)
        square = num * num
        return square
    except ValueError:
        n_num = num * len(num)
        return n_num

Hey Andreas, that code should work, but it is hard to read because you didn't refactor. Code should be short and read-able!

Thanks, Alex

Yes Alex, I am aware code should be refactored. All i was doing was editing the original code posted. If i was writing my own code it would have written differently. And i think its more important to understand the topic first, writing better python i.e good practices can come later. Oh well each person has there own approaches to coding.

Well in my opinion I answer with short answers so that people can also start to learn about refactoring.

Refactoring makes files take up less memory, makes it easier to read, and also faster to run (when you are working with big files). It also just in general makes it cleaner and easier to change/manage the code.

Hope this helps :)

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Sorry, Alexander Davison, but I have to strongly agree with Andreas cormack. Code in Treehouse challenges should NOT be refactored. First, this could throw a "Bummer" in the challenge, especially if you edit (refactor) any of the pre-loaded code. Second, and more importantly, Code Challenges are designed to be at the coding level to which the student is currently at in the course. There is a high probably that refactoring has not been taught or introduced. By improperly introducing refactored code would likely add much confusion to the learning process. So, no, I never refactor code I am helping other students with in challenges either. :dizzy: