-1

I am trying to replace different letters of the alphabet for a value: vowels are 1; "b", "g", "r" or "x" are 3 and the rest of the alphabet equals 5 (this is each character).

I've tried to use convert_text and sum but both didn't work. Also, if there is a way of simplyfing "rest" (all characters - vowel - points) would be appreciated!

I'm new at this, so thank you for your help!

convert_text = input("Enter a string: ")
vowel= "a" or "e" or "i" or "o" or "u"
points= "b" or "g" or "r" or "x"
rest= ¡ "c" or "d" "f" or "h" or "j" or "k" or "l" or "m" or "n" or "p" or "q" or "t" or "s" or "u"or "v" or "w" or"y" or "z"

replacements= {vowel:1, points:3, rest:5}

converted_text = input.sub('(\d+)', lambda m: replacements[m.group()], sentence)

print(converted_text)

For example "hello" would translate to 17 or "overflow" into 26. Thanks again!

CGG
  • 67
  • 7
  • 3
    `vowel= "a" or "e" or "i" or "o" or "u"` doesn't do what you want it to. It's just going to assign the value `'a'` to `vowel`. Same goes for `points` and `rest`. – Random Davis Feb 12 '19 at 19:24

2 Answers2

3

One way to do this:

score = 0
for c in convert_text:
    if c in ['a', 'e', 'i', 'o', 'u']:
        score += 1
    elif c in ['b', 'g', 'r', 'x']:
        score += 3
    else:
        score += 5
print(score)
Rylan Polster
  • 321
  • 3
  • 14
2

Try encoding your score logic in a function, like so:

def score(letter):
    if letter in ('a', 'e', 'i', 'o', 'u'):
        return 1
    if letter in ('b', 'g', 'r', 'x'):
        return 3
    return 5

Then you can combine it with sum to do your computation like so:

convert_text = input("Enter a string: ")
sum(score(letter) for letter in convert_text)

for example:

>>> convert_text = input("Enter a string: ")
Enter a string: 'asdf'
>>> sum(score(letter) for letter in convert_text)
16

(For completeness, you may want to add some handling for non-letter characters, such as numbers, spaces, and punctuation.)

chris
  • 1,915
  • 2
  • 13
  • 18