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”
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%.
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) + "%")
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) + "%")