如果在求和之前从ord [a]中减去96,使用ord [a]查找单词分数会返回不同的答案?

时间:2018-12-11 01:22:07

标签: python-2.7

我正在尝试遍历列表中的单词,并根据其字母为每个单词创建一个分数。

执行此操作时,如果我将每个字母+96的值保留在字母index中默认字母(a would be 1 but ord[a] is 97 by default)的上方,我得到的数字将不同于首先减去96的数字? / p>

这是我的意思:

def high(x):
word = []
for i in x.split(' '):
    score = 0
    for w in i:
        score += ord(w) - 96
    word.append([score, str(i) ])
print max(word)
return max(word)[1]

high('what time are we climbing up the volcano')
从此代码中删除max(word)时,

- 96返回一个不同的值。答案是火山,但返回攀登

1 个答案:

答案 0 :(得分:0)

我认为这就是您想要的:

def high(sentence):
    words = sentence.split()
    scores = []
    for word in words:
        score = 0
        for character in word:
            score += ord(character) - 96
        scores.append(score)
    index = scores.index(max(scores))
    return words[index]

print(high('what time are we climbing up the volcano'))

哪些印刷品:

volcano