给出下面的代码-2个字典和for循环。
如果我取消缩进行sim_choice = raw_input("\nPlease enter item code for the SIM card you want?: ")
,则其上方的打印语句print(" " + str(value))
将打印sims词典中的所有值,但else语句将变为无效。当前输出仅打印sims中的第一个值?例如:
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?:
我看不到为什么它不会在当前状态下打印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]))
答案 0 :(得分:0)
这是因为<div id="box">box</div>
是一个阻塞语句,这意味着它将在此处停止您的代码,直到发生事件(在这种情况下为用户输入)为止。
要解决您的raw_input
语句的问题,应取消缩进以使其与else
对应的语句对齐。
答案 1 :(得分:0)
我认为您只需要删除最后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]))