I'm making a calculator for a class assignment and my program works but I want it to be more efficient / well designed. I'd like to be able to create a 'template' function for a calculation but right now my code is quite linear in that I've repeated the 'calculating' segments to account for every possible equation. So far my program allows for up to 3 numbers and 2 operators. Here's my code:
if operator1 == "+":
if operator2 == "+":
prevInput = (num1 + num2 + num3)
elif operator2 == "-":
prevInput = (num1 + num2 - num3)
elif operator2 == "/":
prevInput = (num1 + (num2 / num3))
elif operator2 == "x" or operator2 == "*":
prevInput = (num1 + (num2 * num3))
elif operator1 == "-":
if operator2 == "+":
prevInput = (num1 - num2 + num3)
elif operator2 == "-":
prevInput = (num1 - num2 - num3)
elif operator2 == "/":
prevInput = (num1 - (num2 / num3))
elif operator2 == "x" or operator2 == "*":
prevInput = (num1 - (num2 * num3))
elif operator1 == "/":
if operator2 == "+":
prevInput = ((num1 / num2) + num3)
elif operator2 == "-":
prevInput = ((num1 / num2) - num3)
elif operator2 == "/":
prevInput = (num1 / (num2 / num3))
elif operator2 == "x" or operator2 == "*":
prevInput = (num1 / (num2 * num3))
elif operator1 == "x" or operator1 == "*":
if operator2 == "+":
prevInput = ((num1 * num2) + num3)
elif operator2 == "-":
prevInput = ((num1 * num2) - num3)
elif operator2 == "/":
prevInput = (num1 * (num2 / num3))
elif operator2 == "x" or operator2 == "*":
prevInput = (num1 * (num2 * num3))
elif not(num2 == ""):
num1, num2 = float(num1), float(num2)
if operator1 == "+":
prevInput = (num1 + num2)
elif operator1 == "-":
prevInput = (num1 - num2)
elif operator1 == "/":
prevInput = (num1 / num2)
elif operator1 == "x" or operator1 == "*":
prevInput = (num1 * num2)
FYI this part is within a function itself and the prevInput variable is printed at the end of the function, but I believe the embedding of another function could be utilised in this case. Any suggestions with how I could create a default calculation template? or am I stuck with my linear format? Sorry if this seems simple, I'm only a freshman in CS.
The first thing you can do to improve your code is to use a loop. Simply repeatedly check for input, determine whether or not you are expected an operator or a number, and act accordingly. You can then maintain a running result of the calculation and have the ability to take in equations of any length. Your code might look something like this (this is Python "pseudocode"):
while user_is_not_done_inputting:
if expected_operator and user_input is a valid operator:
current_operator = operator
elif expected_number and user_input is a valid number:
result = apply_operator(result, operator, user_input)
check_for_next_user_input()
if expecting_operator:
print("ERROR: Unfinished Equation")
else:
print(result)
This way, you only have to actually write the calculation logic once (you don't even need a function although one would be more idiomatic and elegant). The same applies to other aspects of your program (I would write functions but that is up to you).
Note: result, user_input, and current_operator would need to exist outside the loop.
Depending on the requirements of the assignment, the easiest way would be to use eval(), like this:
x = input("Enter expression: ")
print(eval(x))
eval() takes a string as an input and evaluates the expression the string represents, so this method will work for any amount of operators as long as it's entered like python code.
If you just want to loop through operators an numbers, you can do that pretty simply with a lookup and a loop. The lookup maps a symbol to a function, then look them up and call them. Something like:
import operator
ops = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv
}
operats = ['+', '*', '+', '+']
nums = iter([2, 6, 10, 2, 4])
res = next(nums, 0)
for n, o in zip(nums, operats):
res = ops[o](res, n)
print(res)
# 86
This of course assumes you want all the operations in the order defined rather than some predefined operator precedence.
Related
So I wrote a basic calculator in python:
import math
import sys
num1 = float(input("Enter a number "))
operator = input("Enter a operator ")
num2 = float(input("Enter a second number "))
if operator == '+':
print (num1) + (num2)
elif operator == '-':
print(num1 - num2)
elif operator == '*':
print(num1 * num2)
elif operator == '/':
print(num1 / num2)
elif operator == "sqrt":
print(math.sqrt(num1))
else:
print("Unknown operator")
I want to make the square root command function so that when I choose num1 and the square root operator it skips the rest and gives me the results directly. Instead of needing to also write out num2.
Put it in a function so you can return before asking for num2 if appropriate:
import math
def calc():
num1 = float(input("Enter a number "))
operator = input("Enter a operator ")
if operator == "sqrt":
print(math.sqrt(num1))
return
num2 = float(input("Enter a second number "))
if operator == '+':
print (num1) + (num2)
elif operator == '-':
print(num1 - num2)
elif operator == '*':
print(num1 * num2)
elif operator == '/':
print(num1 / num2)
calc()
Other unary operations can simply be added as elifs under the one for sqrt.
Another approach might be to put your operators into dicts according to the number of operands:
import math
unary_ops = {
"sqrt": math.sqrt,
}
binary_ops = {
"*": float.__add__,
"-": float.__sub__,
"*": float.__mul__,
"/": float.__truediv__,
}
num1 = float(input("Enter a number "))
operator = input("Enter a operator ")
if operator in unary_ops:
print(unary_ops[operator](num1))
elif operator in binary_ops:
num2 = float(input("Enter a second number "))
print(binary_ops[operator](num1, num2))
else:
print(f"Sorry, I don't know how to '{operator}'.")
I wanted to have some sort of thing that if the user inputs "Quit", the program breaks. I think that I could achieve that with a do while loop but I don't understand how to implement it. Please help me.
num1 = float(input("Enter First Number: "))
num2 = float(input("Enter second number: "))
op = input("Enter Operator: ")
if op == "*":
print(num1 * num2)
elif op == "+":
print(num1 + num2)
elif op == "-":
print(num1 - num2)
elif op == "/":
print(num1 / num2)
elif op == "%":
print(num1 % num2)
else:
print("Invalid Operator")
while True:
try:
#insert your "loop" here
[.....]
except ValueError:
#restart the loop
continue
else:
#exit the loop
break
while True:
while True:
num1 = input("Enter First Number: ")
if num1.lower() == "quit":
quit()
try:
num1 = int(num1)
break
except ValueError:
print(f"{num1} is not an integer.")
while True:
num2 = input("Enter second number: ")
if num2.lower() == "quit":
quit()
try:
num2 = int(num2)
break
except ValueError:
print(f"{num2} is not an integer.")
op = input("Enter Operator: ")
if op.lower() == "quit":
quit()
if op == "*":
print(num1 * num2)
elif op == "+":
print(num1 + num2)
elif op == "-":
print(num1 - num2)
elif op == "/":
print(num1 / num2)
elif op == "%":
print(num1 % num2)
else:
print("Invalid Operator")
Code works pretty well if you enter an actual number and operator and it actually gives u an error code if you enter an invalid operator but I want to give same error code with numbers but I dont know how I use python 3.9 help plz
num1 = float(input('Enter First Number:'))
op = input('Enter operator:')
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('enter an operator plz')
You want to use try/catch statements and/or while loops around each step.
flag1,flag2,operator =True,True,True
while flag1:
try:
num1 = float(input('Enter first Number:'))
flag1 = False
except ValueError:
print("Enter a valid first number please")
while operator:
op = input('Enter operator:')
if not (op=='+' or op =='-' or op =='*' or op=='/'):
print('Enter a valid operator please')
else:
operator=False
while flag2:
try:
num2 = float(input('Enter second Number:'))
flag2 = False
except ValueError:
print("Enter a valid second number please")
if op == '+':
print(num1 + num2)
elif op == '-':
print(num1 - num2)
elif op == '*':
print(num1 * num2)
elif op == '/':
print(num1 / num2)
Maybe try something along the lines of this:
invalid = True
while invalid:
try:
num1 = float(input('Enter First Number:'))
op = input('Enter operator:')
num2 = float(input('Enter Second Number:'))
invalid = False
except ValueError:
print("Please enter a valid number")
if op == '+':
print(num1 + num2)
elif op == '-':
print(num1 - num2)
elif op == '*':
print(num1 * num2)
elif op == '/':
print(num1 / num2)
else:
print('enter an operator plz')
Why does "Invalid Operator" print when the if statement is false? Could I use an elif statement to make it work?
num1 = float(input("Enter the first number: "))
operator = input("Enter your operator here: ")
num2 = float(input("Enter your second number here: "))
if operator == "+":
print(num1 + num2)
if operator == "-":
print(num1 - num2)
if operator == "*":
print(num1 * num2)
if operator == "/":
print(num1 / num2)
if operator != ("+", "-", "*", "/"):
print("Invalid Operator.")
You are compairing an operator to a tuple. Instead, you can check if operator in tuple, like this:
if operator not in ("+", "-", "*", "/"):
print("Invalid Operator.")
The alternative is elifs:
if operator == "+":
print(num1 + num2)
elif operator == "-":
print(num1 - num2)
elif operator == "*":
print(num1 * num2)
elif operator == "/":
print(num1 / num2)
else:
print("Invalid Operator.")
With this, when you add a new operator you don't need to modify the not in (...) condition.
The last if-statement is not correct. Now it checks if it is equal to the entire tuple. You could check if the operator is not in the list.
num1 = float(input("Enter the first number: "))
operator = input("Enter your operator here: ")
num2 = float(input("Enter your second number here: "))
if operator == "+":
print(num1 + num2)
if operator == "-":
print(num1 - num2)
if operator == "*":
print(num1 * num2)
if operator == "/":
print(num1 / num2)
if operator not in ("+", "-", "*", "/"):
print("Invalid Operator.")
Another option is indeed using elif and else. If it’s not one of the first for options, then in will be an Invalid Operator (else).
if operator == "+":
print(num1 + num2)
elif operator == "-":
print(num1 - num2)
elif operator == "*":
print(num1 * num2)
elif operator == "/":
print(num1 / num2)
else:
print("Invalid Operator.")
Edit: Changed if not in ... from list to tuple. Tuples are more efficient in this context.
operator != ("+", "-", "*", "/") # condition_1
where operator is a string object, where as ("+", "-", "*", "/") is a tuple so both are not equal so above condition_1 result into True and last if block will get executed.
Either use if/elif/else statement or change last condition correctly like this
operator not in ("+", "-", "*", "/") # itemwise ckech takeplace
operator not in "+-*/" # string is also a iterable object
This program is supposed to be a calculator. When I run the program it prints operation as this
I'm not sure how to fix this. I have been trying to make a simple calculator that runs in terminal for a few days now but nothing seems to work. I think I need to re-define the operation var to print it. I'm not sure how to do that.
#The functions of this program #
def add(num1, num2):
return (num1 + num2)
def sub(num1,num2):
return (num1 - num2)
def mul(num1, num2):
return (num1 * num2)
def div(num1, num2):
return (num1 / num2)
##The variables of this program ##
num1 = input ("Number 1: ")
num2 = input ("Number 2: ")
operation = input ("Operation: ")
###The if statements of this program ###
if operation == "add":
(num1 + num2)
elif operation == "sub":
(num1 - num2)
elif operation == "mul":
(num1 * num2)
elif operation == "div":
(num1 / num2)
####The final code to print the product ####
print operation
You didn't call your functions in your if statements
if operation == "add":
print(add(num1, num2))
elif operation == "sub":
print(sub(num1, num2))
elif operation == "mul":
print(mul(num1, num2))
elif operation == "div":
print(div(num1, num2))
Also note that you can use a dict to grab the function and evaluate it
ops = {'add': add,
'sub': sub,
'mul': mul,
'div': div}
if operation in ops:
print(ops[operation](num1, num2))
else:
print('Invalid operator requested')
There are some issues in your code:
You're not calling the functions you defined.
You're not printing the result, you're printing the operator (and not even correctly).
You're applying the operations on string not numbers (input returns a string).
Use raw_input instead of input in Python-2.x.
A solution:
#The functions of this program #
def add(num1, num2):
return (num1 + num2)
def sub(num1,num2):
return (num1 - num2)
def mul(num1, num2):
return (num1 * num2)
def div(num1, num2):
return (num1 / num2)
##The variables of this program ##
num1 = float(raw_input("Number 1: ")) # convert the input to float
num2 = float(raw_input("Number 2: ")) # convert the input to float
operation = raw_input("Operation: ")
# The result variable, it holds an error message, in case the use inputs another operation
result='Unsupported operation'
###The if statements of this program ###
if operation == "add":
result = add(num1, num2)
elif operation == "sub":
result = sub(num1, num2)
elif operation == "mul":
result = mul(num1, num2)
elif operation == "div":
result = div(num1, num2)
####The final code to print the product ####
print result
Here is what you need .............
python 3 and the following code
Make sure you are using python3 and not python
e.g. root#Windows-Phone:~$ python3 anyName.py
#The functions of this program
def add(num1, num2):
return (num1 + num2)
def sub(num1,num2):
return (num1 - num2)
def mul(num1, num2):
return (num1 * num2)
def div(num1, num2):
return (num1 / num2)
#The variables of this program
num1 = int(input ("Number 1: "))
num2 = int(input ("Number 2: "))
operation = input ("Operation: ")
#The if statements of this program
if operation == "add":
print(add(num1, num2))
if operation == "sub":
print(sub(num1 ,num2))
if operation == "mul":
print(mul(num1,num2))
if operation == "div":
print(div(num1,num2))