if structure converted to dictionary - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/Forum-General) +--- Forum: Tutorials (https://python-forum.io/Forum-Tutorials) +---- Forum: Fundamentals (https://python-forum.io/Forum-Fundamentals) +---- Thread: if structure converted to dictionary (/Thread-if-structure-converted-to-dictionary) if structure converted to dictionary - metulburr - Jul-15-2019 If you do not know what a python dictionary is please read this tutorial first. This tutorial is explained here as well. But is more geared towards gaming. This is aimed to be generic and simple. This example is taken from a user on this forum. This is a simple illustration of how to use dictionaries to avoid massive if/elif structures. The following example is a simple calculator taking input for the both operands and the operator of an arithmetic expression. In this tutorial we are not going to eval or the operator module. This is because not all if conditions are arithmetic. However this if/elif structure can pertain to anything else. This is just the example that is used as it is less complicated for this tutorial. In this example the if conditions would run out at some point. However, in real coding conditions there are times when you could keep adding a condition to the if structure as time passes almost unlimited times. Imagine if you had 250+ if conditions, etc. This might look easier, but it is drawn out. Every time you add or remove a condition you have to modify the if structure directly. It is also a lot of extra repetitive code (which python always strives to remove). ```number1 = int(input("What is the first number?")) operator = input("What do you want to do + Plus - Minus * Multiply / Divide or p Power of? ") number2 = int(input("What is the second number?")) if operator == "+": answer = number1+number2 elif operator == "-": answer = number1-number2 elif operator == "*": answer = number1*number2 elif operator == "/": answer = number1/number2 elif operator == "Plus": answer = number1+number2 elif operator == "Minus": answer = number1-number2 elif operator == "Multiply": answer = number1*number2 elif operator == "Divide": answer = number1/number2 elif operator == "p": answer = number1**number2 elif operator == "power of": answer = number1**number2 print("The answer is " + str(answer))```This code is cluttered with a long if/elif condition. It is repetitive and ugly. It is more likely to contain bugs than the later option here. First we are going to make the operator dictionary. ```operators = { '+':plus, '-':minus, '/':division, '*':multiplication }```This is the bare bones of what we are going to use and expand upon it to be more adaptable. In this case we can can call the dictionary `operators['+']()` to call the plus function. Assuming `plus` is a function this is the same as calling `plus()`. In this way we only need to change the string given to the operators dictionary to change the function executed, which is given by the input of the user in this example. Here we have a partial example ```def plus(a,b): return a + b def minus(a,b): return a - b def division(a,b): return a / b def multiplication(a,b): return a * b def power(a,b): return a ** b operators = { '+':plus, '-':minus, '/':division, '*':multiplication, '**':power } number1 = int(input("What is the first number?")) op = input("What do you want to do +,-,*, **, or /") number2 = int(input("What is the second number?")) result = operators[op](number1, number2) print(result)```This works if given the correct input ``````Output:What is the first number?14 What do you want to do +,-,*, **, or /+ What is the second number?14 28 ``````To accommodate the original post requirements we only need to add to the dictionary. ```def plus(a,b): return a + b def minus(a,b): return a - b def division(a,b): return a / b def multiplication(a,b): return a * b def power(a,b): return a ** b operators = { '+':plus, '-':minus, '/':division, '*':multiplication, '**':power, 'plus':plus, 'divide':division, 'division':division, 'mult':multiplication, 'power':power } number1 = int(input("What is the first number?")) op = input("What do you want to do +,-,*, **, or /") number2 = int(input("What is the second number?")) result = operators[op](number1, number2) print(result)```Notice that there are no if conditions at all. But yet it does the same thing. It is much cleaner and pythonic. It is much easier to read, more likely to be bug free, and more maintainable later on if needed. ``````Output:What is the first number?13 What do you want to do +,-,*, **, or /plus What is the second number?100 113 ``````Yes this example does have more lines than the original. However as the if/elif condition expands to be longer and longer, the dictionary way will be the obvious concise method. Usually people do not bother with a few if conditions, but you still can. For example this code replaces if conditions with a dictionary for movement of an RPG game. Yet there are only 4 keys in the dictionary. Then instead of repeating code 4 times, it only has to be done once. Otherwise it would have to be repeated for every direction. If you are interested in validating the users input you would combine methods described in this tutorial.