-1

I feel like the answer to this will be pretty obvious but I'm wondering why when I run intialize() and enter A, B or C it still prints ['warehouse d'], and I've ran into the same problem in the past where it was just printing ['warehouse a']. I'm trying to get it so that a user selects the list they wish to append some data later to but it is always picking either WHA or WHD and nothing else.

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)

2 Answers2

2

You'll want to make a separate equality comparison for your or statement.

if choice == 'a' or choice == 'A':

Joshua M. Moore
  • 401
  • 6
  • 18
  • This. You can avoid doing multiple comparisons based on letter case using `if choice.lower() == 'a'` or `if choice.upper() == 'A' ` – jhomr Nov 26 '18 at 23:21
  • ```(choice == 'a' or 'A')``` is `True` when your first conditional is hit, because `choice == 'a'` is `False`, but `'A'` is truthy. So the first conditional is executed and `WHA` is assigned to `selectedWH`. The same thing happens for the second conditional, then the third, then the fourth. – Max Gasner Nov 26 '18 at 23:21
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()])
Django Doctor
  • 8,450
  • 9
  • 45
  • 66