[英]Why is only the first value in my dictionary printing
Given the code below - 2 dictionaries and for loops.给定下面的代码 - 2 个字典和 for 循环。
If I un-indent the line sim_choice = raw_input("\\nPlease enter item code for the SIM card you want?: ")
the print statement above it print(" " + str(value))
prints all the values in the sims dictionary but then the else statement becomes void.如果我取消缩进
sim_choice = raw_input("\\nPlease enter item code for the SIM card you want?: ")
上面的打印语句print(" " + str(value))
打印 sims 字典中的所有值但随后 else 语句变为无效。 Currently the output is only printing the first value in sims?目前输出只打印sims中的第一个值? example:
例子:
Please tell me your name? X
Hello X
Please enter item code for the phone or tablet you want?: BPCM
Item Code Chosen BPCM['Phone', 'Description: Compact', 'price', 29.99]
As you chose a phone, would you like a SIM free or Pay As You Go for - ['Phone', 'Description: Compact', 'price', 29.99]
Item Code: SMNO
['SIM card', 'Description: SIM Free (no SIM card purchased)', 'price', 0.0]
Please enter item code for the SIM card you want?:
I cannot see why it wont print all values from sims dictionary in its current state?我不明白为什么它不会在当前状态下打印 sims 字典中的所有值?
device = {
'BPCM' : ['Phone', 'Description: Compact', 'price', 29.99],
'BPSH' : ['Phone', 'Description: Clam Shell', 'price', 49.99],
'RTMS' : ['Tablet', 'Description: RoboTab - 10-inch screen and 64GB memory', 'price', 299.99],
'RTLM' : ['Tablet', 'Description: RoboTab - 10-inch screen and 256 GB memory', 'price', 499.99],
}
sims = {
"SMNO" : ['SIM card', 'Description: SIM Free (no SIM card purchased)', 'price', 0.00],
"SMPG" : ['SIM card', 'Description: Pay As You Go (SIM card purchased)', 'price', 9.99],
}
print("Hello customer, here is your list of phones or tablets. Please choose a phone or tablet by entering your name and the item code:")
for key, value in device.items():
print("\nItem Code: " + key)
print(" " + str(value))
name = raw_input("\nPlease tell me your name? ")
print("Hello " + name)
device_choice = raw_input("Please enter item code for the phone or tablet you want?: ")
if device_choice in device.keys():
print("Item Code Chosen " + device_choice + str(device[device_choice]))
sim_cards = ['BPCM', 'BPSH']
if device_choice in sim_cards:
print("\nAs you chose a phone, would you like a SIM free or Pay As You Go for - " + str(device[device_choice]))
for key, value in sims.items():
print("\nItem Code: " + key)
print(" " + str(value))
sim_choice = raw_input("\nPlease enter item code for the SIM card you want?: ")
else:
print("Item Code Chosen " + device_choice + str(device[device_choice]))
That is because raw_input
is a blocking statement, meaning it will stop your code there until an event (user input in this case) happens.那是因为
raw_input
是一个阻塞语句,这意味着它会在那里停止你的代码,直到一个事件(在这种情况下是用户输入)发生。
To fix the problem with your else
statement, you should un-indent it to be aligned with its if
counterpart.要解决
else
语句的问题,您应该取消缩进以与其if
对应项对齐。
I think you just need to remove an indent from the last 3 lines我认为您只需要从最后 3 行中删除缩进
if device_choice in sim_cards:
print("\nAs you chose a phone, would you like a SIM free or Pay As You Go for - " + str(device[device_choice]))
for key, value in sims.items():
print("\nItem Code: " + key)
print(" " + str(value))
sim_choice = raw_input("\nPlease enter item code for the SIM card you want?: ")
else:
print("Item Code Chosen " + device_choice + str(device[device_choice]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.