Home > Net >  Can I use something else to shorten the code rather than using mutiple IF in python?
Can I use something else to shorten the code rather than using mutiple IF in python?

Time:08-30

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()

CodePudding user response:

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.

CodePudding user response:

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.

  • Related