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
John Hill
Front End Web Development Techdegree Graduate 35,236 PointsMade 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
Treehouse Guest TeacherInteresting idea! What made you want to make this?
David McCarty
5,214 PointsThat'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.
John Hill
Front End Web Development Techdegree Graduate 35,236 PointsAlright -- excellent! I see how that cleans it up a bit. Is it more time efficient this way?
David McCarty
5,214 PointsIf 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. :)
John Hill
Front End Web Development Techdegree Graduate 35,236 PointsCool. That's what I was getting at, more efficient from a processor perspective. Thanks again for the suggestion and explaining that!
David McCarty
5,214 PointsAnytime man! Happy to help!
A really cool idea btw!
John Hill
Front End Web Development Techdegree Graduate 35,236 PointsJohn Hill
Front End Web Development Techdegree Graduate 35,236 PointsFigured 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!"