Making a calculator
- 라임 샹큼
- Feb 28
- 4 min read
When playing with numbers, a calculator is always needed. Why not make one myself?
There are many ways to make a calculator but I used newfound knowledge of dictionaries to make this calculator.
Making process
To reduce the amount of code I would need to write, I made functions for each arithmetic operation.
def add(n1, n2):
return n1 + n2
def subtract(n1, n2):
return n1-n2
def multiply(n1,n2):
return n1 *n2
def divide(n1,n2):
return n1/n2
After, I used dictionaries to store the newly made functions. This is a new skill I learned:
def add(n1, n2):
return n1+n2
my_fav = add
print(my_fav(2,3))
So I implemented this in the same way:
operation = {
'+': add,
'-':subtract,
'*':multiply,
'/':divide
}
The next thing I needed to do was actually make the calculator function with the new functions I had made.
I learned through this project that I didn't need to add arguments to new functions. I could simply make it so that I could call for an input and implement it later on in the function.
I wanted to make sure the user could either continue with their calculation or start a wholly new calculation. So I wanted to use the while loop. However my big error was that I used two while loops for each instance I wanted the the user to be able to experience. At first the code worked well but it suddenly stopped working at a certain point.( I am still not entirely sure why this is)
value =0
while restart == 'yes':
num1 = float(input('input first number'))
ask_operation = input('''+
-
*
/\n''')
num2 = float(input('input second number'))
value = operation[ask_operation](num1, num2)
print(f'{num1} {ask_operation} {num2} = {value}')
restart = input('do you wish to restart or continue? yes for restart no for continue:\n').lower()
while restart =='no':
print('\n'*100)
num3 = float(input('input second number'))
ask_operation = input('''+
-
*
/\n''')
value = operation[ask_operation](value, num3)
print(f'{value} {ask_operation} {num3} = {value}')
restart = input('do you wish to restart or continue? yes for restart no for continue:\n').lower()
Eventually I rewrote the code so that it only had 1 loop. I also added the newly made calculation function in the making of the calculation function to make it into a sort of loop.
def calculator():
should_accumulate = True
num1 = float(input('input number:\n'))
while should_accumulate:
num2 = float(input('input number:\n'))
for op in operation:
print(op)
selected_operation = input('select operation:\n')
result = operation[selected_operation](num1, num2)
print(f'{num1} {selected_operation} {num2} = {result}')
choice = input('if you wish to continue with result type y, if you want to restart type n').lower()
if choice == 'y':
num1 = result
else:
should_accumulate = False
print('\n'*100)
calculator()
This was not easy to think of and many of my early ideas had faults. I realized I still have a long way to go.
The finished code looks something like this:
def add(n1, n2):
return n1 + n2
def subtract(n1, n2):
return n1-n2
def multiply(n1,n2):
return n1 *n2
def divide(n1,n2):
return n1/n2
operation = {
'+': add,
'-':subtract,
'*':multiply,
'/':divide
}
def calculator():
should_accumulate = True
num1 = float(input('input number:\n'))
while should_accumulate:
num2 = float(input('input number:\n'))
for op in operation:
print(op)
selected_operation = input('select operation:\n')
result = operation[selected_operation](num1, num2)
print(f'{num1} {selected_operation} {num2} = {result}')
choice = input('if you wish to continue with result type y, if you want to restart type n').lower()
if choice == 'y':
num1 = result
else:
should_accumulate = False
print('\n'*100)
calculator()
Other things I learned:
fruits = {'a': 'yes', 'b':'banana'}
print(max(fruits, key=fruits.get)
finds maximum value if value is numeral
for key in dict_:
print(key)
print(dict_[key])
->prints both key and value
dict:
for thing in dict_:
print(thing)
->gives only keys not the descriptions
dict_name ={
'hi':'hi is a form of greeting',
'hello': 'hello is a longer form of hi',
}
- indent the elements in dict
- add comma after element
- add last brace at the start of new line
Comments