2017-09-16 61 views
1

我很新的編程所以請原諒我如果有什麼是沒有意義的,或者如果我的字的東西不正確。我有一個關於使用元組的字典鍵的問題。使用元組作爲密鑰在Python

首先,我有用戶輸入的號碼,

num = input("Enter a number here: ") 

然後,我把這個數字值轉換成元組:

numTup = tuple(num) 

接下來,創建連接數字鍵字的字典值:

numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"} 

最後,我希望它打印對應的字典值t o元組中的鍵。我敢肯定,這就是我得到它錯了。

print(numWords[numTup]) 

基本上我想要這個程序做的是有它打印每個用戶輸入的數字作爲一個詞,即。 456將變成「四五六」。

完整(不正確)的腳本:

num = input("Enter a number here: ") 
numTup = tuple(num) 
numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"} 
print(numWords[numTup]) 
+0

目前還不清楚爲什麼你把它們變成元組。在字典中的鍵是字符串,而不是數字或元組。你從'input'得到的東西也是一個字符串。你可以直接使用它作爲一個鍵,你已經設置它的方式,而不需要將它轉換成任何東西。 – pvg

+0

哦,我明白你的意思,你希望輸入中的每個字符都可以用作關鍵字。但是你試圖用元組本身作爲關鍵字進行字典查找。所以如果一個元組是'('3','1','0')',你的查找就是尋找那個確切的鍵,而不是'3','1','0'鍵。您需要迭代元組的值並將這些值(它們是字符串)用作關鍵字。由於你的字典有鍵字符串。 – pvg

+0

所以,這裏的元組實際上是多餘的,你可以簡單地迭代你得到的字符串的字符作爲輸入。所以'inputtring:'中的數字將啓動這樣一個循環。然後在循環中,您可以打印'numWords [digit]' – pvg

回答

3

tuple沒有必要在這種情況下,因爲你的字典會將鍵分配給關聯的字符串。

num = input("Enter a number here: ") 

numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"} 
for n in num: 
    print(numWords[n], end=' ') 

演示:

Enter a number here: 456 
Four Five Six 
+0

該列表似乎沒有比元組更有必要。 – pvg

+0

的確你是對的 – davedwards

+0

可能還想添加一些'splainin'。 – pvg

-1

你爲什麼要轉成數元組目前尚不清楚,但如果你想要的,你在一個錯誤的方式做。當你想創建一個int元組,你應該做到以下幾點:

numTup = (num) 

而且完整的代碼應該是這樣的:

num = input("Enter a number here: ") 
numTup = (num) 
numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"} 
print(numWords[numTup]) 
0

還有就是你的字典的鍵type和將其轉換爲一個元組tuple(num)之後的輸入之間的不匹配。 您既可以直接跳過部分,你轉換成元組:

num = input("Enter number here: ") 
num = input("Enter a number here: ") 
numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"} 
print(numWords[num]) 

OR

索引,元組和採摘第0個元素訪問元素:

num = input("Enter number here: ") 
num = input("Enter a number here: ") 
numTup = tuple(num) 
numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"} 
print(numWords[numTup[0]]) 

注意:確保用於訪問字典項目的鍵和變量的數據類型相同。您可以使用type()命令檢查變量的數據類型。

0
''' 
You can print the number in words given irs numeric 
version if by defining your dictionary with 
the following format: 
    dict{'number': 'word'} 

Then, calling the dictionary as follows: 
    dict['number'] 
''' 

# Number (keys) and words (values) dictionary 
numWords = {'1': "One", '2': "Two", 
'3': "Three", '4': "Four", 
'5': "Five", '6': "Six", 
'7': "Seven", '8': "Eight", 
'9': "Nine", '10': "Ten"} 

# Function that prints the number in words 
def print_values(): 
    for k,v in numWords.items(): 
     print(v) 

''' 
Alternatively, if you want to print a value using 
its key as an argument, you can use the following 
funciton: 
''' 

# Interactive function to print number in words 
# given the number 
def print_values2(): 
    key = input("What number would you like to print? ") 
    print(numWords[key]) 

''' 
P.s. if you want to add a number to your dictionary 
you can use the following function 
''' 

# Function to modify numWords 
def add_number(): 

    # Specify the number you want to add 
    numeric = input("Type in the number: ") 

    # Input the number in words 
    word = input("Write the number in words: ") 

    # Assign number and word to dictionary 
    numWords[numeric] = word 

    # Return modified dictionary 
    return numWords 
+0

這聽起來有點像這個問題的模仿企業解決方案。 – pvg