gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:46:21 25 4
gpt4 key购买 nike

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.

关于python - 此代码片段不适用于大写和小写字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17261180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com