1

So I've built a simple calculator in Python where the user inputs two numbers and an operator and the answer is given. They also have the option to run the calculator again. I want to number the answers so each answer reads "Answer 1 equals x", "Answer 2 equals x", etc. depending on how many times the calculator is run. Everytime I try to format the counter to count iterations, it won't work and is stuck just labeling them "Answer 1" over and over. Any help would be greatly appreciated. I'm super new to Python.

answer = "y"

while ((answer == "Y") or (answer == "y") or (answer == "Yes") or (answer == "yes")):
    numones = input ("Give me a number: ")
    numtwos = input ("Give me another number: ")

    numone = float(numones)
    numtwo = float(numtwos)

    operation = input ("Give me an operation (+,-,*,/): ")

    counter = 0
    for y in answer:
        counter += 1

    if (operation == "+"):
        calc = numone + numtwo
        print ("Answer " + str(counter) + " is " + str(calc))
    elif (operation == "-"):
        calc = numone - numtwo
        print ("Answer " + str(counter) + " is " + str(calc))
    elif (operation == "*"):
        calc = numone * numtwo
        print ("Answer " + str(counter) + " is " + str(calc))
    elif (operation == "/"):
        calc = numone / numtwo
        if (numtwo != 0):
            print ("Answer " + str(counter) + " is " + str(calc))
        else:
            print ("You can't divide by zero.")
    else:    
        print ("Operator not recognized.")

    answer = input ("Do you want to keep going? ")
    if ((answer == "Y") or (answer == "y") or (answer == "Yes") or (answer == "yes")):
        print ()
    else:
        print ("Goodbye.")
        break
DSM
  • 342,061
  • 65
  • 592
  • 494

1 Answers1

3

Remove assigning counter = 0 in your while loop. And move this declaration above your while loop.

also lines:

for y in answer:
    counter += 1

are really confusing and sure are wrong, because if you got 'yes' as an answer you would get +3 increase. Just increment(counter += 1) counter without any for-loop.

vishes_shell
  • 22,409
  • 6
  • 71
  • 81