Home > OS >  Assuming each letter in a word has an assigned number, like 'a' = 3, 'b' = 2, ho
Assuming each letter in a word has an assigned number, like 'a' = 3, 'b' = 2, ho

Time:10-26

For example:

Welcome to the Scribble Scorer Enter word: mostboringest
Word: mostboringest Score: 32.5

Each letter in mostboringest has a number value. The program added up all the values and printed a score.

How can I code this in Python using while loops and for loops?

Here is what I have so far:

user_input = input('Welcome to the Scribble Scorer; Enter Word:')

i = {'e': 1, 't': 1.5, 'a': 2, 'i': 2, 'n': 2, 's': 2, 'h': 2.5, 'r': 2.5, 'd': 2.5, 'l': 3, 'u': 3, 'c': 3.5, 'm': 3.5, 'f': 4, 'w': 4.5, 'y': 4.5, 'p': 5, 'g': 5, 'b': 5.5, 'v': 5.5, 'k': 5.5, 'q': 5.5, 'j': 6, 'x': 6, 'z':6}

Prompt the user for a word (you can just ask them for any string). For this program, we will score lower case letters only!

Using a loop, calculate the score for the word (string) provided. You don't need to check for errors (like specifying more than one word), but your program should not crash or fail if given an incorrect input. All point values are given in the comments in the code cell for you. If you find a character other than the ones below, assign it a value of -1 point.

Print the word and the score.

Keep repeating these steps until the user enters nothing ('').

CodePudding user response:

like 'a' = 3, 'b' = 2

mapping = {'a': 3, 'b': 2}
mapping['a']   mapping['b']  # = 5

CodePudding user response:

One way to achieve this is by using a dictionary which maps letters to a specific weight value. With this, you can iterate through your string and add up the total value accumulated for every character in the string.

letterValues = {
    "a": 1,
    "b": 2,
    "c": 3,
    "g": 1,
    "t": 6
    # .. and so on.
}

string = "mostboringest"

totalValue = 0
for char in string: # For every character in our string.
    if char in letterValues: # If this character has a defined weight value.
        totalValue  = letterValues[char] # Add it to our total.

print(totalValue)
  • Related