calculator.py A simple calculator program.

def main(): print ("CALCULATOR")

    operation = eval(input("Type the operation you want to perform:"))

    print ("The answer is:", operation)

main () 

1 Answers

0
Devesh Kumar Singh On Best Solutions

Just wrap is around a infinite loop, one way of doing it is while True

def main():
    print ("CALCULATOR")

    operation = eval(input("Type the operation you want to perform:"))

    print ("The answer is:", operation)

#Infinite loop
while True:
    main()

This will give you

CALCULATOR
Type the operation you want to perform:5+7
The answer is: 12
CALCULATOR
Type the operation you want to perform:4*6
The answer is: 24
CALCULATOR
Type the operation you want to perform:

In order to break the while True loop at some point, you would want to come up with a word, say quit and when the use types it, you exit out of the infinite loop using exit()

def main():
    print ("CALCULATOR")

    #Take operation from user
    operation = input("Type the operation you want to perform, else type quit to exit:")

    #If quit is typed exit
    if operation == 'quit':
        exit()

    #Else evaluate and print result
    operation = eval(operation)
    print ("The answer is:", operation)

#Infinite loop
while True:
    main()

This will give you

CALCULATOR
Type the operation you want to perform, else type quit to exit:6+7
The answer is: 13
CALCULATOR
Type the operation you want to perform, else type quit to exit:quit