我觉得答案很明显,但是我想知道为什么当我运行intialize()并输入A,B或C时,它仍然会打印['warehouse d'],而我遇到了过去只是打印['warehouse a']时遇到的相同问题。我正在尝试获取它,以便用户选择他们希望稍后添加一些数据的列表,但是它总是选择WHA或WHD,而没有其他选择。
WHA = ['warehouse a']
WHB = ['warehouse b']
WHC = ['warehouse c']
WHD = ['warehouse d']
def initialize():
choice = input("Please select warehouse A, B, C or D: ")
if choice == 'a' or 'A':
selectedWH = WHA
if choice == 'b' or 'B':
selectedWH = WHB
if choice == 'c' or 'C':
selectedWH = WHC
if choice == 'd' or 'D':
selectedWH = WHD
print(selectedWH)
答案 0 :(得分:2)
您将要为or语句进行单独的相等比较。
if choice == 'a' or choice == 'A':
答案 1 :(得分:0)
mapping = {
'A': WHA,
'B': WHB,
'C': WHC,
'D': WHD
}
def initialize():
choice = input("Please select warehouse A, B, C or D: ")
print(mapping[choice.upper()])