Fatima Asim
Fatima Asim

Reputation: 21

Alternatives to multiple elif statements when taking user input

I wrote a code in python to guess hair colors but it's using multiple elif statements. Can anyone suggest and write an alternative code for me please? The code works fine actually but my teacher said that you should use another code instead of multiple elifs because it looks bad. I tried using def function but it didn't work out. here's the original code.

print('hair color')
print('colors: black,brown,red,blonde,orange' )
user_input1=input('enter hair color 1:')
user_input2=input('enter hair color 2')
if  user_input1=='orange' and user_input2=='orange':
    print('orange hair')
elif user_input1=='red' and user_input2=='brown':
    print('deep golden brown hair')
elif user_input1=='brown' and user_input2=='black':
    print('dark brown hair')
elif user_input1=='brown' and user_input2=='brown':
    print('brown hair')
elif user_input1=='brown' and user_input2=='blonde':
    print('golden brown hair')
elif user_input1=='black' and user_input2=='red':
    print('dark red hair')
elif user_input1=='blonde' and user_input2=='black':
    print('brown hair')
elif user_input1=='blonde' and user_input2=='blonde':
    print('blonde hair')
elif user_input1=='black' and user_input2=='black':
    print('black hair')
elif user_input1=='black' and user_input2=='orange':
    print('brown hair')
elif user_input1=='brown' and user_input2=='orange':
    print('copper blonde hair')
elif user_input1=='red' and user_input2=='blonde':
    print('strawberry blonde hair')
elif user_input1=='red'  and user_input2=='orange':
    print('ginger beer hair')
elif user_input1=='blonde' and user_input2=='orange':
    print('light orange hair')
elif user_input1=='red' and user_input2=='red':
    print('red hair')
else:
    print('ERROR 404 HAIR COLOR NOT FOUND!')

Upvotes: 0

Views: 110

Answers (2)

jgphilpott
jgphilpott

Reputation: 649

I would suggest you use a 'switch' referred to as Structural Pattern Matching in Python, see the documentation. In your case it would look something like this because you are matching multiple conditions:

match (user_input1, user_input2):
    case ('orange', 'orange'):
        print('orange hair')
    case ('red', 'brown'):
        print('deep golden brown hair')
    case ('brown', 'black'):
        print('dark brown hair')
    case _:
        print('ERROR 404 HAIR COLOR NOT FOUND!')

Note: Pattern matching is only available in Python 3.10 and above.

Upvotes: 0

NIKUNJ KOTHIYA
NIKUNJ KOTHIYA

Reputation: 2165

Yes, you can reduce multiple if else conditions by map into a dictionary inside python.

For that your new solution will be :

colors = {
    ('orange', 'orange'): 'orange hair',
    ('red', 'brown'): 'deep golden brown hair',
    ('brown', 'black'): 'dark brown hair',
    ('brown', 'brown'): 'brown hair',
    ('brown', 'blonde'): 'golden brown hair',
    ('black', 'red'): 'dark red hair',
    ('blonde', 'black'): 'brown hair',
    ('blonde', 'blonde'): 'blonde hair',
    ('black', 'black'): 'black hair',
    ('black', 'orange'): 'brown hair',
    ('brown', 'orange'): 'copper blonde hair',
    ('red', 'blonde'): 'strawberry blonde hair',
    ('red', 'orange'): 'ginger beer hair',
    ('blonde', 'orange'): 'light orange hair',
    ('red', 'red'): 'red hair',
}

print('hair color')
print('colors: black,brown,red,blonde,orange' )
user_input1 = input('enter hair color 1:')
user_input2 = input('enter hair color 2:')

result = colors.get((user_input1, user_input2))

if result:
    print(result)
else:
    print('ERROR 404 HAIR COLOR NOT FOUND!')

Sample Result :

hair color
colors: black,brown,red,blonde,orange
enter hair color 1:red
enter hair color 2:orange
ginger beer hair

Upvotes: 1

Related Questions