0

Need a loop to read each character of a string (a string that was inpt by the user) and then add up numbers assignd to letters.

I'm trying to take a string that is input by the user, then read each character of the string. I'm pretty new to this but I understand the key is going to be a function that uses a loop. I also want to assign a variable to hold a count of values assinged to letters a-z and combine the total value of all the letters in a string. Example: F = input("Enter a Word: ") print (F) r = 0 for i in F: r += 1 print (r) This prints for me the word (N) and prints the length (r). I'm having a hard time finding the best way of taking the input apart and seeing if i can read the first letter of a word (eg; apple), and lets say if a is read, it adds 15 to a variable (or 16, any number for a). Then after it reads the first, it goes through the rest of characters, and adds different values dependant on what letter is read. Any and all suggestions, please. I may not understand everything yet but whatever you can tell me about this idea would be much appreciated.

16th Dec 2020, 8:50 PM
jonathan
jonathan - avatar
2 ответов
0
Here's a start:- import string mydict = {x: ord(x)-81 for x in string.ascii_letters} print(mydict) This gives "a as 16" (and b as 17 etc) ...just change the '81' as you need.
16th Dec 2020, 9:17 PM
rodwynnejones
rodwynnejones - avatar
0
I would probably look to Pandas. Maybe something like this helps? https://code.sololearn.com/cuajqX5t13Qk/?ref=app
16th Dec 2020, 9:29 PM
Steven M
Steven M - avatar