从用户输入打印值

时间:2016-03-10 10:38:18

标签: python dictionary

我有一本像这样的字典,

replies = {'0':'Leave it out to dry for 24 hours, if it does not work see customer service',
           '1':'Go to a repair store to get it fixed',
           '2':'Charge the phone battery',
           '3':'Try resetting the phone',
           '4':'Check to see if something blocking the audio jack or the speakers',
           }

我有int(input(,我希望用户输入一个数字来打印字典中的值,我试过print(replies[number the user input])

但它不起作用,感谢任何帮助,也不是我不是很有经验,所以简单有帮助,谢谢

3 个答案:

答案 0 :(得分:0)

如果您想使用数字(整数)作为密钥,您需要获得如下数据:

replies = {0: 'Leave it out to dry for 24 hours, if it does not work see customer service',
           1: 'Go to a repair store to get it fixed',
           2: 'Charge the phone battery',
           3: 'Try resetting the phone',
           4: 'Check to see if something blocking the audio jack or the speakers',
           }

在这种情况下,它可能更好地使用列表并通过索引访问您的数据。

答案 1 :(得分:0)

replies = {'0':'Leave it out to dry for 24 hours, if it does not work see customer service',  
               '1':'Go to a repair store to get it fixed',  
               '2':'Charge the phone battery',  
               '3':'Try resetting the phone',  
               '4':'Check to see if something blocking the audio jack or the speakers',
               } 

你的索引不是整数,这就是为什么你可以说打印回复[1]

只需将字典更改为

replies = {0:'Leave it out to dry for 24 hours, if it does not work see customer service',
           1:'Go to a repair store to get it fixed',
           2:'Charge the phone battery',
           3:'Try resetting the phone',
           4:'Check to see if something blocking the audio jack or the speakers',
           }

答案 2 :(得分:0)

这个正在运作....试试这个......

 input1=raw_input("Enter number ")
 print replies[input1]

您正在尝试将输入键转换为整数....但是回复字典键是在字符串....