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

Made a 'Magic Number' script this morning...

I made a short script that adds up words and numbers while ignoring white space and punctuation. For example: 'john' = 47, 'TeamTreehouse' = 155, 'python3' = 101.

Check it out and let me know if you have any feedback!

print("Welcome! Enter a word or phrase to calculate its Magic Number (oooo...!).  Press '!!!' to exit.")
print("***********************************************************************************************")

while True:
    word = input("Word(s) to calculate: ").lower()
    nopunc = ""
    output = []

    if word == "!!!":
        break

    for char in word:
        if char.isalpha():
            nopunc = "".join([nopunc,char])
            number = ord(char) - 96
            output.append(number)
        elif char.isnumeric():
            nopunc = "".join([nopunc,char])
            output.append(int(char))

    print(sum(output))

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Interesting idea! What made you want to make this?

Figured I keep with the fantasy/magic vibe and try a task that mixed in some covered material but still made me search a bit. How that popped in my head? Man, if I only knew...! But it helped me grasp some fundamentals of the logic and I picked up a few new tools.

Great vids by the way -- big thanks to you for keeping it fun and challenging. "No more dark basements for me!"

That's pretty cool!

My only suggestion is to get rid of the output variable and just change it to an int that you add to instead of appending to the output.

So instead of doing output.append(number)

You could just have a variable named "sum" that, at the end of each conditional statement does

sum += number

and

sum += int(char)

That way you don't have to sum the output list at the end.

Alright -- excellent! I see how that cleans it up a bit. Is it more time efficient this way?

If by time efficient you mean there's less for the computer to process than yes!

What this basically does is make it so the computer doesn't have to do a bunch of appending to a list and then go back through each element of the list and add them all together.

Instead, with the above method, the adding is done on the fly and all the computer has to do in the return statement is just pass the variable containing the number back.

So, yes, this way is a tad bit more efficient. :)

Cool. That's what I was getting at, more efficient from a processor perspective. Thanks again for the suggestion and explaining that!

Anytime man! Happy to help!

A really cool idea btw!