# Python — Nested Ifs Nov 2019

NESTED IFs: A question was asked recently on Stack Overflow

The solution shown below is readable, scalable, easily edited — and contains no IF statements.

This is similar to the method proposed by Anitoliy R except I use a single string and the split function to simplify editing and scaling.

To illustrate scalability, I add 3 new data types ‘J’, ‘K’, and ‘M’ with added random return values.

``````
PYTHON CODE

numeric_types='Hexadecimal Decimal Binary J K M'.split()

#                       H   D   B   J   K   M
conversion_list_2D = [[ 0,  1,  2, 11, 20, 19],
[ 4,  0,  6, 28, 10, 30],
[ 5,  3,  0, 15, 29, 14],
[ 8, 16, 17,  0, 27, 26],
[18,  9, 24, 22,  0, 25],
[12, 21, 13, 23,  7,  0]]

def convert_what(numeral_sys_1, numeral_sys_2):
try:
q1 = numeric_types.index(numeral_sys_1)
q2 = numeric_types.index(numeral_sys_2)
return conversion_list_2D[q1][q2]
except ValueError:
return False

# let's try a few tests
print(convert_what('Decimal', 'Binary'))
print(convert_what('Binary', 'Decimal'))
print(convert_what('Binary', 'Q'))
print(convert_what('Binary', 'K'))

PYTHON OUTPUT

1
2
4
6
5
3
False
29
```
```