我正在学习使用Python创建词典,我创建了下面的词典,以便在当地警察局的预订中心获取被拘留者的信息。该信息是用户输入的:
local_police = {'Name' : " ",'Phone number' : '','Address' : " ", 'Registered' : ' ','posession' : []} """a dictionary for the suspects details"""
local_police['Name']=raw_input("Enter suspects name:")
local_police['Phone Number']=input("Enter phone number:")
local_police['Address']=raw_input("Enter Address:")
local_police['Registered']=raw_input("Has suspect been registered? Y/N:")
local_police['posession']=raw_input("items in suspect's backpack (seperate each item with a comma):")
print local_police
我注意到我无法在字典中的“电话号码”键中成功输入任何值。有人可以解释一下原因吗?
编辑:我将通过添加以下内容进一步澄清帖子:
我的预期输出如下:
Enter suspects name:thief dude
>>> local_police['Address']=raw_input("Enter Address:")
Enter Address:theif house
>>> local_police['Registered']=raw_input("Has suspect been registered? Y/N:")
Has suspect been registered? Y/N:Y
>>> local_police['posession']=raw_input("items in suspect's backpack (seperate each item with a comma):")
items in suspect's backpack (seperate each item with a comma):gun,knife,puppy
>>> local_police['Phone Number']=raw_input("Enter phone number:")
Enter phone number:111111
>>> print local_police
{'Phone number': '', 'Name': 'thief dude', 'Registered': 'Y', 'Phone Number': '111111', 'posession': 'gun,knife,puppy', 'Address': 'theif house'}
您会注意到,在打印时,会打印两个键用于电话号码。一个值为111111(用户输入的值),另一个为空:
{'Phone number': '', 'Name': 'thief dude', 'Registered': 'Y', 'Phone Number': '111111', 'posession': 'gun,knife,puppy', 'Address': 'theif house'}
答案 0 :(得分:1)
我认为你的问题是电话号码是在字典中用小写字母n写的,但是当你要求输入时,它是用大写字母n写的电话号码。