Reputation: 23
I decided that I wanted to take on a python project to help hone my skills, so I decided to make a python calculator. I was able to get everything up and running, but I am currently running into a problem when I try to multiply or divide through the main function. Right now I have figured out that which ever one comes first it will make the other one do what it is intended to do. For example.. When i try to divide but the multi function is above the div function my divide will turn out multiplied...
I know that there might be some easy way to solve this, but i can't seem to see it! So any help would be much appreciated!
Also any other suggestions to improve my code are welcome!
'''
import sys
test = [10, 20]
def add(*arg):
result = 0
for x in arg[0]:
result += x
print(result)
def sub(*arg):
arg = arg[0]
result = arg[0]
for x in arg[1:]:
result -= x
print(result)
def multi(*arg):
arg = arg[0]
result = arg[0]
for x in arg[1:]:
result *= x
print(result)
def div(*arg):
arg = arg[0]
result = arg[0]
for x in arg[1:]:
result /= x
print(result)
def main():
while True:
math_tool = input('What would you like to do: ').lower()
if math_tool == 'exit':
sys.exit()
else:
math_choice = \
list(map(int, input('What would you like to' +
f' {math_tool}: ').split(',')))
if math_tool == 'add':
add(math_choice)
elif math_tool == 'sub':
sub(math_choice)
elif math_tool == 'div' or 'divide':
div(math_choice)
elif math_tool == 'multi' or 'multiply':
multi(math_choice)
else:
print('error')
# print(math_choice, ' Type:', type(math_choice[0]))
main()
'''
Upvotes: 0
Views: 132
Reputation: 1
Here is my answer:-
num1 = ""
op = ""
num2 = ""
while op != "*" or op != "+" or op != "-" or op != "/":
num1 = float(input("Enter first number: "))
op = input("Enter operation: ")
num2 = float(input("Enter second number: "))
if op == "*":
print("Answer =")
print(num1 * num2)
print("\n")
elif op == "+":
print("Answer =")
print(num1 + num2)
print("\n")
elif op == "-":
print("Answer =")
print(num1 - num2)
print("\n")
elif op == "/":
print("Answer =")
print(num1 / num2)
print("\n")
else:
print("Invalid Operation")
print("\n")
Upvotes: 0
Reputation: 1480
You missed a condition just add a condition in or
you have to check math_tool
is equal to the string otherwise condition is always true.
elif math_tool == 'div' or math_tool == 'divide':
div(math_choice)
elif math_tool == 'multi' or math_tool == 'multiply':
multi(math_choice)
Upvotes: 2