This question already has an answer here:
I'm making a calculator for a class project, but I cannot figure out why whenever I run the program, python ignores the input phrases and considers any possible variable or input to all equal the same thing.
def addition(num1, num2):
return num1 + num2
def subtraction(num1, num2):
return num1 - num2
def multiplication(num1, num2):
return num1 * num2
def division(num1, num2):
return num1 / num2
def modulus(num1, num2):
return num1 % num2
def exponents(num1, num2):
return num1 ** num2
#a = 'a'
#s = 's'
#m = 'm'
#d = 'd'
#add = "add"
#sub = "sub"
#mult = "mult"
#div = "div"
inNum1 = 3.456#float(input("What is the first number? -----> "))
print
inNum2 = 1.234#float(input("What is your second number? ---> "))
print
print
print("Which operation do you want to perform on these numbers? ")
print("Addition (add), Subtraction (sub), Multiplication (mult), Division (div)")
print("Modular Arithmetic (mod), Exponential Multiplication (expo)")
operator = 's'#str(input("Choice here: "))
operator = str(operator)
print(operator)
operator = operator.lower()
print(operator)
if(operator == "add" or 'a'):
#print(str(inNum1) + " - " + str(inNum2) + " = " + str(subtraction(inNum1, inNum2)))
print (str(inNum1) + " + " + str(inNum2) + " = " + str(addition(inNum1, inNum2)))
elif operator == "sub" or 's':
print(str(inNum1) + " - " + str(inNum2) + " = " + str(subtraction(inNum1, inNum2)))
elif operator == "mult" or 'm':
print(str(inNum1) + " * " + str(inNum2) + " = " + str(multiplication(inNum1, inNum2)))
elif operator == "div":
print(str(inNum1) + " / " + str(inNum2) + " = " + str(division(inNum1, inNum2)))
elif operator == "mod":
print(str(inNum1) + " % " + str(inNum2) + " = " + str(modulus(inNum1, inNum2)))
elif operator == "expo":
print(str(inNum1) + " ** " + str(inNum2) + " = " + str(exponents(inNum1, inNum2)))
I know it is a problem in the if statement because I ran a debugger and it got confused at the first if statement even if you hardcode the operator variable for one of the calculation options. I know you can much more easily right a block of if, elif statements to make a simple calculator, but I was just playing around with it and ran into this problem
Aucun commentaire:
Enregistrer un commentaire