Python Forum

Full Version: how do i repeat???
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have some coding that i would like to repeat here is the coding how do i do it??? it is for a calculator and i want to repeat the last bit ("main()")
#returns the result of adding num1 + num2
def add(num1, num2):
    return num1 + num2

#returns the result of subtracting num1 - num2
def minus(num1, num2):
    return num1 - num2

#returns the result of mutipliying num1 * num2
def multiply(num1, num2):
    return num1 * num2

#returns the result of dividing num1 / num2
def divide(num1, num2):
    return num1 / num2


def main():
    operation = input("what do you want to do (+,-,*,/): ")
    if(operation != '+' and operation != '-' and operation != '*' and operation != '/'):
        #invalid operation
        print("please enter a vaild operation")
    else:
        num1 = int(input("Enter num1: "))
        num2 = int(input("Enter num2: "))
        if(operation == '+'):
            print(add(num1, num2))
        elif(operation == '-'):
            print(minus(num1, num2))
        elif(operation == '*'):
            print(multiply(num1, num2))
        else:
            print(divide(num1, num2))
            
main()
Hello and welcome to the forum!
I see you are already familiar with if/elif/else statements. Next very useful thing to learn can be loops, and there you will also find (at least one) solution for your challenge. Good luck!
See if you can pick up some hint in my post here.