我一直坚持这一段时间。基本上get_text_value(text)下面的代码被传递一串单词,它提取每个单词的第一个和最后一个字母,将其与letter_values匹配,并根据匹配的第一个和最后一个单词的总和给出最终值。到目前为止,我只能“提取”整个字符串的第一个和最后一个字母,而不是每个单词。 我知道我打算将字符串转换为列表,但我不完全确定如何去做。任何帮助将不胜感激。谢谢!Python - 需要从字符串中提取第一个和最后一个字母
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