2017-09-16 45 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

相关问题