Skip to content
Advertisement

Calculator not functioning properly

Hey I’m trying to make a small calculator for a project, however I’m struggling to get the code to work.

import time

a = 100
b = 1

while a > b:
     print("Please select an operation. ")
     print("1. Multiply")
     print("2. Divide")
     print("3. Subtract")
     print("4. Add")
     b = 200
     x = int(input("Please enter your operation number. "))
     if x == 4:
          y = int(input("Please enter your first number. "))
          z = int(input("Please enter your second number. "))
          finaladd = (y+z)
          print(finaladd)
          c = input("Would you like to do another calculation? ")
     if c == 'yes' or c == 'Yes':
           a = 500
     if c == 'no' or 'No':
           b = 1000

if x == 2:     
     y2 = int(input("Please enter your first number. "))
     z2 = int(input("Please enter your second number. "))
     finaladd2 = (y2/z2)
     print(finaladd2)

if x == 3:
     y3 = int(input("Please enter your first number. "))
     z3 = int(input("Please enter your second number. "))
     finaladd3 = (y3-z3)
     print(finaladd3)

if x == 1:
       y4 = int(input("Please enter your first number. "))
       z4 = int(input("Please enter your second number. "))
       finaladd4 = (y4*z4)
       print(finaladd4)

When I try to run operation 4, I want the code to ask if you would like to run another calculation, if you answer with ‘yes’ it will repeat the statement again. However all it does is stop the code. Sorry if it’s obvious I’m quite new to coding.

Advertisement

Answer

Try this…

import time

a = 100
b = 1

while a > b:
    print("Please select an operation. ")
    print("1. Multiply")
    print("2. Divide")
    print("3. Subtract")
    print("4. Add")
    b = 200
    x = int(input("Please enter your operation number. "))
    if x == 4:
        y = int(input("Please enter your first number. "))
        z = int(input("Please enter your second number. "))
        finaladd = (y+z)
        print(finaladd)
        c = input("Would you like to do another calculation? ")
        if c == 'yes' or c == 'Yes':
            a = 500
        elif c == 'no' or c == 'No':
           b = 1000
   
    elif x == 2:     
         y2 = int(input("Please enter your first number. "))
         z2 = int(input("Please enter your second number. "))
         finaladd2 = (y2/z2)
         print(finaladd2)
    
    elif x == 3:
         y3 = int(input("Please enter your first number. "))
         z3 = int(input("Please enter your second number. "))
         finaladd3 = (y3-z3)
         print(finaladd3)
    
    elif x == 1:
           y4 = int(input("Please enter your first number. "))
           z4 = int(input("Please enter your second number. "))
           finaladd4 = (y4*z4)
           print(finaladd4)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement