Reputation: 21
There is a list with words (wordListReal). Every letter in the alphabet has an assigned value (see code).
Now the question is how to obtain the value of all the letter values added together per word, then divided by the number of letters in that word.
I tried this, but strangely enough it returns only lots of 0, 1 and 2, which is incorrect:
# wordListReal = ["mystery", "strange", "why", "how", "thinking", "solution", "gratitude", "life"]
frequencyPointList = []
points1 = 0
points2 = 0
for word1 in wordListReal:
points1 = 0
for letter1 in word1:
if letter1 in {"e" , "a" , "r" , "i" , "o"}:
points1 += 1
elif letter1 in {"t" , "n" , "s" , "l" , "c"}:
points1 += 2
elif letter1 in {"u" , "d" , "p" , "m" , "h"}:
points1 += 3
elif letter1 in {"g" , "b" , "f" , "y" , "w"}:
points1 += 4
elif letter1 in {"k" , "v" , "x" , "z" , "j" , "q"}:
points1 += 5
numberLetters1 = len(word1)
points2 = points1 // numberLetters1
frequencyPointList.append(points2)
print(frequencyPointList)
Eventually the results need to be in a list, not in a dictionary.
Many thanks in advance!
Edit: through in between printing it becomes more clear where the problem lies, but not how to solve it yet. Points1 and numberLetters1 are calculated correctly and they are both integers, but when dividing them through each other to arrive at points2 something goes wrong!
wordListReal = ["mystery", "strange", "why", "how", "thinking", "solution", "gratitude", "life"]
frequencyPointList = []
points1 = 0
points2 = 0
for word1 in wordListReal:
points1 = 0
for letter1 in word1:
if letter1 in {"e" , "a" , "r" , "i" , "o"}:
points1 += 1
elif letter1 in {"t" , "n" , "s" , "l" , "c"}:
points1 += 2
elif letter1 in {"u" , "d" , "p" , "m" , "h"}:
points1 += 3
elif letter1 in {"g" , "b" , "f" , "y" , "w"}:
points1 += 4
elif letter1 in {"k" , "v" , "x" , "z" , "j" , "q"}:
points1 += 5
print("points1: " + str(points1))
print(type(points1))
numberLetters1 = len(word1)
print("numberLetters1: " + str(numberLetters1))
print(type(numberLetters1))
points2 = (int(points1) // int(numberLetters1))
print("points2: " + str(points2))
print(type(points2))
frequencyPointList.append(points2)
Upvotes: 2
Views: 193
Reputation: 4391
Couple of things that seem odd to me:
elif
instead of if
as this will speed up the process. A letter can only match one time.EDIT: changed the logic a bit and used the provided word list as an example
def get_points_for_word(word: str) -> tuple:
total_points = 0
for letter in word:
if letter in "eario":
total_points += 1
elif letter in "tnslc":
total_points += 2
elif letter in "udpmh":
total_points += 3
elif letter in "gbfyw":
total_points += 4
elif letter in "xvxzjq":
total_points += 5
return (total_points, total_points / len(word))
wordListReal = [
"mystery",
"strange",
"why",
"how",
"thinking",
"solution",
"gratitude",
"life",
]
print("Word | TOT | AVE ")
print("---------------------+-----+-----")
for word in wordListReal:
total_points, average_points = get_points_for_word(word)
print(f"{word:20} | {total_points:3} | {average_points:.3}")
This gives the output below:
Word | TOT | AVE
---------------------+-----+-----
mystery | 17 | 2.43
strange | 13 | 1.86
why | 11 | 3.67
how | 8 | 2.67
thinking | 15 | 1.88
solution | 14 | 1.75
gratitude | 18 | 2.0
life | 8 | 2.0
Upvotes: 1
Reputation: 3852
You just need to keep track of your variables:
points1
should always be 0
at the begining of your loopletter1
looppoints1
)This means:
for word1 in wordListReal:
points1 = 0
for letter1 in word1:
# if ...
# points1 += ...
toDivideBy = len(word1)
points1 = (int(points1) // int(toDivideBy))
frequencyPointList.append(points1)
At the moment, your code does not reflect this logic, instead it is forcing the first value to always be the same and misses the value of the last word.
Some tips to improve your code further:
if
/elif
(and else
if necessary) to avoid calculating conditions multiple times when you don't need. For example, you have multiple if
statements and all of them will be calculated on each iteration. Using if
/elif
the calculation will stop the first time that condition is True
.print
to check how your code is progressing, for example: print the current word and in the inner loop print the letter and current points1
total to confirm that the total matches your expectations. You could also print the value of frequencyPointList
at the end of each iteration in your outer loop.Upvotes: 1