0

This is a calculator that is capable of calculating two numbers.

On line 3 and 6, I wanted to make python say "Please enter a number: " until the consumer enters a number but somehow even I enter a number, python does not continue the process. Probably because of while num1 != float: and while num2 != float:. But I'm not sure how to fix it even if I'm right.

num1 = input("Enter a number: ")
while num1 != float:
    input("Please enter a number: ")
num2 = input("Enter a number: ")
while num2 != float:
    input("Please enter a number: ")
op = input("What operator would you use?")
if op == "+":
    print(float(num1) + float(num2))
elif op == "-":
    print(float(num1) - float(num2))
elif op == "*":
    print(float(num1) * float(num2))
elif op == "/":
    print(float(num1) / float(num2))
else:
    print("Invalid operator")
  • 1
    in your while loops you have to change the `num2` or `num1` meaning `num1 = input(...` otherwise it loops ad infinum – Derte Trdelnik Jan 25 '19 at 11:52
  • Note that it was never going to equal the type float; input's always a string, and even if it was converted `1.0 != float`. – jonrsharpe Jan 25 '19 at 11:54
  • `num1` and `num2` are strings, so they can never be equal to the _type_ `float`. The pythonic way to dwal with this is to convert the input to a float (`float(num1)`), and if that fails ask again, using `try`, `except`. – Jan Christoph Terasa Jan 25 '19 at 11:55

0 Answers0