dimanche 5 avril 2020

python calculator only adds (if statement dominates other statements)

So I've written some code for a simple calculator that asks for a number, asks for a math operation, asks for a second number, and prints the results. I also added a line to print for an invalid operator input. However, all the code does is the the first IF line. No matter what I add for the operator the results are for addition. Invalid operator input also results in addition.

The code is the following:

\\\
num1 = float(input("Enter first number"))
op = input("What do you want to do?")
num2 = float(input("Enter second number"))
if op == "+":
   print(num1 + num2)
elif op == "-":
   print(num1 - num2)
elif op == "*":
   print(num1 * num2)
elif op == "/":
   print(num1 / num2)
else:
    print("wait a minute! that's not a valid operation dude!")

Example output 1:

Enter first number 1 What do you want to do? - Enter second number 1 2.0

Example output 2 Enter first number 3 What do you want to do? / Enter second number 3 6.0

Example output 3 Enter first number 6 What do you want to do? sleep Enter second number 6 12.0

What am I doing wrong? I don't see any errors in PyCharm. Is it indenting? Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire