从raw_input()添加字典值

时间:2017-02-15 01:55:16

标签: python dictionary

我刚编了一本字典,每个字母都有一个数字值。我尝试了一些东西并且不断出错。我可以使用sum(alphaDict.values())添加字典的所有值,但不能将单独的值与用户输入一起添加。现在,如果我使用下面的代码,则返回的是' str'这些值不能作为数字添加。顺便说一下,我知道这本词典在这篇文章中看起来很有趣,但并不担心。 ^^

alphaDict = {'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}


print 'sum(alphaDict.values()) =', sum(alphaDict.values())


def letter2Num(word = raw_input('Enter a word > ')):
    for char in word:
        print alphaDict[char]



letter2Num()

5 个答案:

答案 0 :(得分:1)

只需这样做:

def letter2Num(word) :
    add = 0
    for char in word :
        add += alphaDict[char]
    return add

print 'Sum :', letter2Num(raw_input('Enter a word > '))

答案 1 :(得分:0)

这应该适用于Python 2.7和3. *:

from sys import exit


alphaDict = {'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}

def letter2Num(word):
    total = 0
    try:
        for char in word:
            total += int(alphaDict[char])
    except KeyError as char:
        print("Invalid char {}".format(char))
        exit(1)
    else:
        print("Total {}".format(total))

letter2Num(word = raw_input('Enter a word > '))

另外,我添加了一个try-except块来避免无效字符。

答案 2 :(得分:0)

你需要按字符遍历整个输入字符,然后得到它的总和。我想最恐怖的方式是:

def letter2Num(word):
    return sum([alphaDict[alpha] for alpha in word])

print("Value: {}".format(letter2Num(raw_input("Enter a word > "))))

答案 3 :(得分:0)

如果用户输入的密钥无效,则使用默认值dictionary.get()尝试这样的操作,返回的值为-1而不是None

alpha_dict = {'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}
print "The dictionary is: %s" % alpha_dict
print "The total of all the values in the dictionary is: %d" % sum(alpha_dict.values())
val_1 = raw_input("Please enter the key for value 1 you want to add? ")
val_2 = raw_input("Please enter the key for value 2 you want to add? ")
print "The sum = %d" % (alpha_dict.get(val_1, -1) + alpha_dict.get(val_2, -1))

使用示例:

The dictionary is: {'e': 5, 'n': 14, 'i': 9, 'z': 26, 'o': 15, 'j': 10, 's': 19, 'f': 6, 'r': 18, 'x': 24, 't': 20, 'l': 12, 'a': 1, 'c': 3, 'u': 21, 'b': 2, 'k': 11, 'm': 13, 'p': 16, 'w': 23, 'd': 4, 'q': 17, 'v': 22, 'h': 8, 'g': 7, 'y': 25}
The total of all the values in the dictionary is: 351
Please enter the key for value 1 you want to add?  y
Please enter the key for value 2 you want to add?  t
The sum = 45

试试here!

答案 4 :(得分:0)

你的功能只是打印每个号码,而且没有返回类型。 如果你想要求和,则返回一个总和。

import string
// zip function is useful to combine same length sequence
alphaDict = dict(zip(string.ascii_lowercase, range(1,27)))

def letter2Num(word):
''' sum numbers according to alphaDict from word'''
    sum = 0
    for char in word:
        if char not in string.ascii_lowercase:
            continue
        sum += alphaDict[char]
    return sum

python不支持使用'int'和'str'的隐式'+'运算符。 如果需要,您需要将str更改为int。 只需使用int()内置函数。

temp ='12345' // which is str type
print type(temp)
temp = int(temp)
print type(int(temp)) // now int type