I've been stuck with this for a while. basically the get_text_value(text)
code below is passed a string of words and it extracts the first and last letter of each word, matches it up with letter_values
and gives the final value based on the sum of the numbers the first and last words matched up with. So far I've only been able to "extract" the first and last letter of the whole string and not each individual word. I know I'm meant to convert the string into a list, but I'm not entirely sure how to go about it. Any help would be greatly appreciated. Thanks!
def get_text_value(text): letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] letter_values = [1, 4, 2, 3, 1, 4, 3, 4, 1, 7, 7, 4, 6, 6, 1, 3, 9, 2, 2, 2, 1, 8, 5, 9, 9, 9] text_value = 0 text = text.lower() for i in text: if text[0] and text[-1] in letters: letters_index_1 = letters.index(text[0]) letters_index_2 = letters.index(text[-1]) text_value = letter_values[letters_index_1] + letter_values[letters_index_2] return text_valuedef test_get_text_value(): print("1. Text value:", get_text_value("abracadabra")) print("2. Text value:", get_text_value("a b")) print("3. Text value:", get_text_value("enjoy Today")) print("4. Text value:", get_text_value("")) text = "Be yourself everyone else is already taken" text_value = get_text_value(text) print("6. Text:", text) print("6. Text value:", text_value)