0

I'm new to python and what I'm trying to do is:

scores per letter:

  • 1 point: e, a, i, o, n, r, t, l, s, u

  • 2 point: d, g

  • 3 point: b, c, m, p

  • 4 point: f, h, v, w, y

  • 5 point: k

  • 8 point: j, x

  • 10 point: q, z

considering the string 'hello' the value in outcome should be: 7 (h= 4, 1= e, 1= l, 1 = o).

my code :

def points_calc2(string):
    points = 0
    for i in string:
       if i == 'e' or 'a' or 'i' or 'o' or 'n' or 'r' or 't' or 'l' or 's' or 'u':
          points += 1
       elif i == 'd' or 'g':
          points += 2
       elif i == 'b' or 'c' or 'm' or 'p':
          points += 3
       elif i == 'f' or 'h' or 'v' or 'w' or 'y':
          points += 4
       elif i == 'k':
          points += 5
       elif i == 'j' or 'x':
          points += 8
       elif i == 'q' or 'z':
          points += 10
return points

thanks for the help!
 
  • 1
    I suggest to build a dictionary that maps each letter to its score i.e. `points = {'a': 1, 'b': 3, ...}` – Pynchia Oct 29 '20 at 21:35
  • `elif i == 'j' or 'x'` --> `elif i == 'j' or i == 'x'` – balderman Oct 29 '20 at 21:56
  • Please don't close questions according to the task that OP is attempting, even if it's a very specific task and there's an exact match. Close questions as duplicates according to the **question that is asked**, or failing that the **problem in the code**. – Karl Knechtel Sep 09 '22 at 08:28

3 Answers3

0

You can make a few lists which would contain certain letters, for example a list which contains letters that count as one point:

tab_onepoint = ['e','a','i','o','n','r','t','l','s','u']

A list which contains letters that count as two points:

tab_twopoint = ['d', 'g']

And so on, and so on. And then you could iterate through the string and check if "i" is in the "onepoint" list, so if it is, +1 would be added to the score. So after iterating through the string "Hello" score would be equal to 4. You can then make the other lists, so it would iterate through them too and it would add to the score adequately.

Full code:

tab_onepoint = ['e','a','i','o','n','r','t','l','s','u']
string = "Hello" 
score = 0
for i in string:
    if i in tab_onepoint:
        score += 1
    elif i in tab_twopoint:
         score+= 2

//edit: Here is also a solution for a dictionary (as one person in comments suggested):

points = {**dict.fromkeys(['e', 'a', 'i', 'o', 'n', 'r', 't', 'l', 's', 'u'], 1), **dict.fromkeys(['d', 'g'], 2), **dict.fromkeys(['b', 'c', 'm', 'p'], 3), **dict.fromkeys(['f', 'h', 'v', 'w', 'y'], 4), 'k': 5, **dict.fromkeys(['j', 'x'], 8), **dict.fromkeys(['q', 'z'], 10)}

string = "Hello"

score = 0
converted_string = string.lower()
for i in converted_string:
    if i in points:
        score = score + points[i]


print(score)

What is "fromkeys" method doing, is basically creating dictionaries inside this one dictionary. So basically, every single letter of 'e', 'a', 'i', 'o', 'n', 'r', 't', 'l', 's', 'u' is becoming a key to the value of 1. The same goes for the rest of the keys and their values. Note that I'm converting the input string to lower letters, just so it will fit with the dictionary's keys.

You just have to pack this solution into a function, and here you go. I guess it would also be okay to make this dictionary global.

Also - you've made a mistake in your spelling. "Hello" gets us a score of 8, you wrote "Helo", which gets us a score of 7.

todovvox
  • 170
  • 10
0

Below
The code uses a dict in order to get the point per char. It also uses the sum function.

lookup = {'e': 1, 'a': 1, 'q': 10, 'z': 10}  # TODO add more
my_string = 'Hello python zozo'
points = sum(lookup.get(x, 0) for x in my_string)
print(points)
balderman
  • 22,927
  • 7
  • 34
  • 52
0

The above answers are probably correct, but I notice you wrote

considering the string 'hello' the value in outcome should be: 7 (h= 4, 1= e, 1= l, 1 = o).

Either it's a typo and you meant the outcome should be 8 (counting the two l's), or you only want to take unique letters into account. In that case:

def points_calc2(string):
    points = 0
    for i in set(string):
       if i in ['e' , 'a' , 'i' , 'o' , 'n' , 'r' , 't' , 'l' , 's' , 'u']:
          points += 1
       elif i in ['d' , 'g']:
          points += 2
       elif i in ['b' , 'c' , 'm' , 'p']:
          points += 3
       elif i in ['f' , 'h' , 'v' , 'w' , 'y']:
          points += 4
       elif i == 'k':
          points += 5
       elif i in ['j' , 'x']:
          points += 8
       elif i in ['q' , 'z']:
          points += 10
    return points