我如何制作一个程序来计算每个单词的值

2024-04-19 18:20:00 发布

您现在位置:Python中文网/ 问答频道 /正文

如果一个单词的值是1,那么就计算出。我已经做了大部分代码,但它不起作用。谁能给我一些建议吗?我最近才开始学习python,所以你能不能尽量详细一点?谢谢。

__author__ = "Anthony Chen"
__copyright__ = "Copyright (C) 2017 Anthony Chen"
__license__ = "Public Domain"
__version__ = "1.0"

output = ('None')
worth = {'a':1,
'b':2,
'c':3,
'd':4,
'e':5,
'f':6,
'g':7,
'h':8,
'i':9,
'j':10,
'k':11,
'l':12,
'm':13,
'n':14,
'o':16,
'q':17,
'r':18,
's':19,
't':20,
'u':21,
'v':22,
'w':23,
'x':24,
'y':25,
'z':26,
}


def findworth():
    for char in wordlist:
        if char in wordlist:
           output = (worth[char])
           wordlist.replace(output) 
        elif worth[char] == False:
            output = (None)

while True:
    output = (None)
    wordlist = []
    word = input(str("Find out how many cents your word is worth. Please enter your word:"))
    word = word.lower()
    wordlist = list(word)
    wordlist = findworth()
    output = sum(wordlist)
    print("Your word's value is:")
    print (output)
    print('.')

这是我运行时显示的:

^{pr2}$

Tags: 代码innoneoutputyouris单词word
3条回答

我的第一条评论是,用于生成worth的方法非常冗长,因此容易出错,实际上,您似乎忘记了'p',而是使用了'o':16。相反,您可以使用字典理解生成价值列表:

worth = {chr(x+96):x for x in range(1,27)}

这是通过在数字1..26上循环,并将n + 96转换为一个字符(使用ASCII表来查看97是'a',98是'b'等等,然后这个字符被用作值的键。在

接下来,我们可以为单词中的每个字符生成一个值列表:

^{pr2}$

这将给我们scores == [8, 5, 12, 12, 5]。在

最后,您可以调用sum函数将列表中的所有值相加:

sum(scores)

返回52。在

您可以将其组合到一个函数中,以获得:

def get_word_score(word):
    worth = {chr(x+96):x for x in range(1,27)}
    scores = [worth[c] for c in word]
    return sum(scores)

或者,这可以通过以下方式在一条线上完成:

sum([ord(c)-96 for c in word])

ordchr相反,它返回给定字符的ASCII值。在

作为使用单词sum([ord(c)-96 for c in word])的演练。第一步是将每个字符放入列表中:

>>> [c for c in 'hello']
['h', 'e', 'l', 'l', 'o']

接下来,将每个字符转换为其ASCII值:

>>> [ord(c) for c in 'hello']
[104, 101, 108, 108, 111]

接下来,通过减去96将每个ASCII值转换为字母表位置:

>>> [ord(c)-96 for c in 'hello']
[8, 5, 12, 12, 15]

最后总结:

>>> sum([ord(c)-96 for c in 'hello'])
52

你的代码功能是:

while True:
    word = input("Find out how many cents your word is worth. Please enter your word:").lower()
    output = sum([ord(c)-96 for c in word])
    print("Your word's value is:", output, ".")

根据您的解决方案(我觉得不是很好,会提供更好的解决方案)

def findworth():
    for i,char in enumerate(wordlist):
        if char in worth:
            output = (worth[char])
            wordlist[i] = (output) 
        else:
            output = (None)

while True:
    output = (None)
    wordlist = []
    word = input(str("Find out how many cents your word is worth. Please enter your word:"))
    word = word.lower()
    wordlist = list(word)
    findworth()
    output = sum(wordlist)
    print("Your word's value is:")
    print (output)
    print('.')

减少冗余是一个更好的解决方案

^{pr2}$

Python的字符串已经是iterable了,因此不必将其放入列表:)

对于避免使用更先进技术的解决方案:

worth = {
    'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7,
    'h':8, 'i':9, 'j':10, 'k':11, 'l':12, 'm':13, 'n':14,
    'o':15, 'q':17, 'r':18, 's':19, 't':20, 'u':21, 
    'v':22, 'w':23, 'x':24, 'y':25, 'z':26}

def findworth(word):
    total = 0
    for char in word:
        if char in worth:
            total += worth[char]
    return total

print("Find out how many cents your word is worth.")

while True:
    word = input("Please enter your word: ").lower()
    print("Your word's value is:", findworth(word))

输出如下:

^{pr2}$

注意:worth当前缺少p,给o一个不正确的值,它可能是:

worth = {
    'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7,
    'h':8, 'i':9, 'j':10, 'k':11, 'l':12, 'm':13, 'n':14,
    'o':15, 'p':16, 'q':17, 'r':18, 's':19, 't':20, 'u':21, 
    'v':22, 'w':23, 'x':24, 'y':25, 'z':26}

相关问题 更多 >