Word Score Challenge

word-score

Did you know?


The ASCII code (Pronounced ask-ee) is a code for representing English characters as numbers, with each character assigned a number from 0 to 127. For example, the ASCII code for uppercase M is 77. The extended ASCII contains 256 characters (using numbers from 0 to 255).

To see a list of the most useful ASCII codes you can download our simplified ASCII helpsheet.

Using Python you can easily access ASCII values of a character using the ord() function. For instance ord(“M”) returns 77 and chr(77) returns “M”

Your Challenge

Write a Pyhon script that prompts the end-user to enter a word, e.g. “Hello”
The script should calculate the score of this word using the letter values (A=1, B=2, C=3…)

So if the use inputs the word “Hello” the script should output: 8 + 5 + 12 + 12 + 15 = 52%.

Solution


Hide SolutionView SolutionExtended Solution
This solution uses a for loop to access each letter of the word one letter at a time.
Using the ord() function it returns the ASCII code for the letter.
The ASCII code for A is 65, for B is 66 and so on. So by taking away 64 to the ASCII code we then get A=1, B=2, C=3 etc.

word = input("Type a word").upper()
wordScore=0

for letter in word:
  letterValue=ord(letter) - 64
  wordScore += letterValue
  
print(str(wordScore) + "%")
This solution is exactly the same as the previous one but displays the full formula to calculate the score e.g. “8 + 5 + 12 + 12 + 15 = 52%”. This formula is stored as a string in a variable called addition.

word = input("Type a word").upper()
wordScore=0
addition=""

for letter in word:
  letterValue=ord(letter) - 64
  if (addition==""):
    addition = str(letterValue)
  else:
    addition = addition + " + " + str(letterValue)
  wordScore += letterValue
  
print(addition + " = " + str(wordScore) + "%")
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: , ,