Skip to main content
 首页 » 编程设计

python之此代码片段不适用于大写和小写字母

2025年05月04日11mengfanrong

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,  
         "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,  
         "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,  
         "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,  
         "x": 8, "z": 10} 
 
def scrabble_score(word): 
    count=0 
    word.lower() 
    print word 
    for char in word: 
        count=count+score[char] 
    return count 

我基本上必须接受输入的单词并根据字典计算它的分数。

请您参考如下方法:

此修改后的代码将起作用:

def scrabble_score(word): 
    count=0 
    word = word.lower() #assign the result of word.lower() to word 

word.lower() 返回修改后的单词,它不修改字符串 inplace。在 Python 中,字符串不可变。 .lower() 返回字符串的事实定义如下:

>>> help(str.lower) 
Help on method_descriptor: 
 
lower(...) 
    S.lower() -> string 
 
    Return a copy of the string S converted to lowercase.