-2

when i run this code it keeps on giving me the answer here is my code

num1 = float(raw_input("enter a number: "))  # type: float
operation = str(raw_input("enter a operation: "))
num2 = float(raw_input("enter a number: "))  # type: float

  while True:
        if operation == "+":
           print num1 + num2
        elif operation == "-":
           print num1 - num2
        elif operation == "*":
           print num1 * num2
        elif operation == "/":
           print (num1 / num2)
        else:
           print("Error Error")

4 Answers4

0

What you might want is to put the input taking code into the while loop:

while True:
    num1 = float(raw_input("enter a number: "))  # type: float
    operation = str(raw_input("enter a operation: "))
    num2 = float(raw_input("enter a number: "))  # type: float
    
    if operation == "+":
        print (num1 + num2)
    elif operation == "-":
        print (num1 - num2)
    elif operation == "*":
        print (num1 * num2)
    elif operation == "/":
        print (num1 / num2)
    else:
        print("Error Error")
Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
0
`while True:` means Infinite Loop.

You can take input inside while loop or you can change the condition of while loop.

Hime
  • 41
  • 5
0

remove the while True: and it will only print out the answer once. while loops continue running as long as the argument is true and True is always true :P

jfdoolster
  • 21
  • 3
0

What you probably want is for the application to keep calculating input from the user.

Try this

def calculate():
    num1 = float(raw_input("enter a number: "))  # type: float
    operation = str(raw_input("enter a operation: "))
    num2 = float(raw_input("enter a number: "))  # type: float
    
    if operation == "+":
        print (num1 + num2)
    elif operation == "-":
        print (num1 - num2)
    elif operation == "*":
        print (num1 * num2)
    elif operation == "/":
        print (num1 / num2)
    else:
        print("Error Error")

while True:
    calculate()