0

I use IF multiple times however there is little change between each if, is there another way to code it to condense the amount line of code to make it look neater?

 def StringAll():
        global print1, print2, print3
        print1,print2,print3 = str(Number1), str(Number2), str(answer)
        return

def MathStuff():
    global MATH, answer
    try: MATH = int(input("1 for +, 2 for -, 3 for *, 4 for/"))
    except ValueError:
        print("Only input number")
        MathStuff()
    if MATH == 1:
        answer = Number1 + Number2
        StringAll()
        print(print1 + " + " + print2 + " = " + print3)
    elif MATH == 2:
        answer = Number1 - Number2
        StringAll()
        print(print1 + " - " + print2 + " = " + print3)
    elif MATH == 3:
        answer = Number1 * Number2
        StringAll()
        print(print1 + " * " + print2 + " = " + print3)
    elif MATH == 4:
        answer = Number1 / Number2
        StringAll()
        print(print1 + " / " + print2 + " = " + print3)
    else:
        print("You can only input number 1-4")
        MathStuff()
  • 1
    What is `Number1` and `Number2`? They are not initialized anywhere in the code. – Cow Aug 30 '22 at 10:26
  • I initialized earlier in the code but couldn't added to my question, it's just Number1/2 = float(input("Input first number")) – Kitty Summoner Aug 30 '22 at 10:30
  • 1
    There is no `switch... case` structure in Python but you can implement your own as outlined in this [answer in the subsection "Using a function to resemble switch...case:"](https://stackoverflow.com/a/37007544/3595907) – DrBwts Aug 30 '22 at 10:40

2 Answers2

0

If you are using the latest python version (3.10), you can use the match case statement.
Have a look at this to get started.

Your function could then look like this:

def MathStuff():
    global MATH, answer
    try: MATH = int(input("1 for +, 2 for -, 3 for *, 4 for/"))
    except ValueError:
        print("Only input number")
        MathStuff()
    match MATH:
        case 1:
            answer = Number1 + Number2
            StringAll()
            print(print1 + " + " + print2 + " = " + print3)
        case 2:
            answer = Number1 - Number2
            StringAll()
            print(print1 + " - " + print2 + " = " + print3)
        case 3:
            answer = Number1 * Number2
            StringAll()
            print(print1 + " * " + print2 + " = " + print3)
        case 4:
            answer = Number1 / Number2
            StringAll()
            print(print1 + " / " + print2 + " = " + print3)
        case _:
            print("You can only input number 1-4")
            MathStuff()

Of course this is only one of many ways to clean up this code (as already pointed out by others here), but at least you will get rid of the many if statements. You could still further clean up from here.
Note: I have not tested this code example.

Andi R
  • 164
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 05 '22 at 00:59
0

Well to reduce the lines means we can not only reduce if else but also optimize other lines that may be unneccesary e.g.

VERSION 1 (Renaming StringAll to Display)

Number1 = float(input('Enter a number 1'))
Number2 = float(input('Enter a number 2'))

def Display(operator, ans):
    print(Number1, operator, Number2, '=', ans)
    return

def MathStuff():
    global MATH, answer
    try: MATH = int(input("1 for +, 2 for -, 3 for *, 4 for/"))
    except ValueError:
        print("Only input number")
        MathStuff()
    if MATH == 1:
        Display('+', Number1 + Number2)
    elif MATH == 2:
        Display('-', Number1 - Number2)
    elif MATH == 3:
        Display('*', Number1 * Number2)
    elif MATH == 4:
        Display('/', Number1 / Number2)
    else:
        print("You can only input number 1-4")
        MathStuff()

Many lines are reduced as we eliminated many variables

VERSION 2 (Even further removing Display function itself)

Number1 = float(input('Enter a number 1'))
Number2 = float(input('Enter a number 2'))

def MathStuff():
    global MATH, answer
    try: MATH = int(input("1 for +, 2 for -, 3 for *, 4 for/"))
    except ValueError:
        print("Only input number")
        MathStuff()
    
    if MATH == 1:
        print(Number1, '+', Number2, '=', Number1 + Number2)
    elif MATH == 2:
        print(Number1, '-', Number2, '=', Number1 - Number2)
    elif MATH == 3:
        print(Number1, '*', Number2, '=', Number1 * Number2)
    elif MATH == 4:
        print(Number1, '/', Number2, '=', Number1 / Number2)
    else:
        print("You can only input number 1-4")
        MathStuff()

or for the ultimate optimization we can even remove these if else for even more shorter approach

VERSION 3 (using eval)

Number1 = input('Enter a number 1')
Number2 = input('Enter a number 2')

mapping = { 1: '+', 2: '-', 3: '*', 4: '/'}

def MathStuff():
    global MATH, answer
    try: MATH = int(input("1 for +, 2 for -, 3 for *, 4 for/"))
    except ValueError:
        print("Only input number")
        MathStuff()
    if MATH >= 1 and MATH <= 4:
        ans = eval(Number1 + mapping[MATH] + Number2)
        print(Number1, mapping[MATH], Number2, '=', ans)
    else:
        print("You can only input number 1-4")
        MathStuff()
MathStuff()

Didn't tested the code, but that's a hint for you after a minor bug fixing just in case if there are you can achieve few lines optimization.

Zain Ul Abidin
  • 2,467
  • 1
  • 17
  • 29