2017-02-15 44 views
-1

我剛製作了一個字典,每個字母都有一個數字值。我嘗試了一些東西,不斷收到錯誤。我可以用sum(alphaDict.values())添加字典的所有值,但不能將單獨的值與用戶輸入一起添加。現在,如果我使用下面的代碼,那麼返回的是'str'值,所以這些值不能作爲數字添加。順便說一下,我知道字典在這篇文章中看起來很有趣,但不用擔心。 ^^從raw_input()添加字典值

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() 
+0

你說「我嘗試了幾件事,並且一直在收到錯誤」,但是你不會告訴我們你嘗試了什麼或者得到了什麼錯誤。我們無法幫助您解決您尚未顯示的問題。請顯示導致您問題的代碼,並顯示它正在引發的異常的完整追溯。否則,我們只會猜測你做錯了什麼。 – Blckknght

回答

1

只要做到這一點:

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

print 'Sum :', letter2Num(raw_input('Enter a word > ')) 
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塊,以避免無效字符。

0

您需要按字符瀏覽整個輸入字符,然後獲取總和。我想最Python的方式是:

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

print("Value: {}".format(letter2Num(raw_input("Enter a word > ")))) 
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!

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不支持用 '詮釋' 和 '海峽' 隱性 '+' 操作。 如果你想,你需要將str改爲int。 只需使用int()內置函數。

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