-1

I have made a "simple" calculator. It works perfectly though no errors but when I execute the .py file and use it in the command prompt it just performs one operation (i.e. addition, subtraction, multiplication or division) and then exits.

Is there some way I could perform more operations without reopening it again and again?

Braiam
  • 1
  • 11
  • 47
  • 78

2 Answers2

2

You could use a while loop and raw_input():

while True:
    operator = raw_input('Do you want to add, subtract, multiply, or divide? ')
    first = raw_input('Enter your first term: ')
    second = raw_input('Enter your second term: ')
    if operator.lower.startswith('a'):
        print first+second
    elif operator.lower.startswith('m'):
        print first*second
    elif operator.lower.startswith('s'):
        print first-second
    elif operator.lower.startswith('d'):
        print float(first)/second
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

You could try this defined method:

def main():
    num1 = raw_input("First Number?: ")
    num2 = raw_input("Second Number?: ")
    opsym = raw_input("Would you like to add, subtract, divide, or multiply?: ")
    if opsym == "add":
        #add
        #printres
        time.sleep(2.5)
        main()
    elif opsym == "subtract":
        #subtract
        #printres
        time.sleep(2.5)
        main()
    elif opsym == "multiply":
        #multiply
        #printres
        time.sleep(2.5)
        main()
    elif opsym == "divide":
        #divide
        #printres
        time.sleep(2.5)
        main()
main()

This is personally easiest for me to keep track of and use. time.sleep() is optional, however, it gives the reader time to note the result before moving on.

x otikoruk x
  • 422
  • 1
  • 6
  • 16
  • This is bad, python doesn't have tail call unrolling, so this will crash at the 1000th operation, when the max program stack limit is reached. – Lie Ryan Mar 19 '15 at 03:42
  • @LieRyan Ever seen a 1000 question math test? Me niether! On a side note, i had never heard of that; then again, id never tested it either :d – x otikoruk x Mar 19 '15 at 04:35