I have looked under several sources and either their explanation is too complex for me to understand or they are solving a different problem.
I need the code to iterate through the characters in a string, ex “what are you doing later today” and return the corresponding number of the alphabet.
I immediately thought Dictionary! and built one…
…and I made a “for” loop to iterate through each letter of the string…
… but I cannot for the life of me get the results of the iteration to properly “get()” the value of the iteration from my dictionary.
Again, using code wars which has a test built into it which I don’t understand how to duplicate in PyCharm to test each section as I go for errors.
def alphabet_position(text):
alphabet = {
'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7,
'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14,
'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21,
'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26
}
for letter in text:
if letter in alphabet:
return alphabet.get(letter)
else:
continue
the test code that is showing on the site is:
from random import randint
test.assert_equals(alphabet_position(“The sunset sets at twelve o’ clock.”), “20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11”)
test.assert_equals(alphabet_position(“The narwhal bacons at midnight.”), “20 8 5 14 1 18 23 8 1 12 2 1 3 15 14 19 1 20 13 9 4 14 9 7 8 20”)number_test = “”
for item in range(10):
number_test += str(randint(1, 9))
test.assert_equals(alphabet_position(number_test), “”)
Where/how do I plug in this test code?
If I replace the function with “phrase = ‘what’s up with you tonight’” it pulls the value from the dictionary just fine.