Knowasiak Community

Register

KNOWASIAK originals - May 7, 2021

If you want to start coding today is not a bad day. Here’s A quick code for begginners.

It a code.

For a quick fact: Coding might have  a future unless a giant meteor strikes earth down and send us all in heaven or hell maybe and atheists idk man they’ll also end up somewhere but that’s not the point right. So if you want to learn coding what should you do?  Practice you dummy. So for your practice I have got a good code for you. Yes basic calculators are easy and boring to make also if you make something cool you can just flex on your friends. So either read the code try it out or just copy paste it to flex on your friends. You can see the code below:

#!/usr/local/env python3
# bodmas_calc.py – A rule-based calculator that follows the rules of BODMAS
# 3+((4+6)*9/(2-16+8*(3^2+7)))/3*6*4-7 – copy and paste this if you can’t think of a calculation yourself
# 7^3+3*(4*5-2^2-28/7+3)+9+((3+5)/(3-1)) – another test equation
# ((3+(2*2)-7)^2)+(((2*2)+3-5)^2)
def solve(calculation):
operator_precedence = {“^”: 4, “/”: 3, “*”: 2, “+”: 1, “-“: 1}
# bodmas_index will store the index of all the operators in the equation
# The order of the list is the order in which the calculation will be performed
bodmas_index = []
for i in range(len(calculation)):
if calculation[i] in operator_precedence:
if len(bodmas_index) == 0: # starts off the bodmas_index list with a value (this is only executed once)
bodmas_index.append(i)
else:
# Loops through the current indexes of operators and works out the order in which the operators need to be
# arranged by comparing the operator_precedence values and seeing if a particular operator needs to be worked on
# before or after another one (e.g all * operations will need to be performed before all – operations)
for x in range(len(bodmas_index)):
if operator_precedence[calculation[i]] < operator_precedence[calculation[bodmas_index[1]]]:
bodmas_index.append(i)
break
elif operator_precedence[calculation[i]] > operator_precedence[calculation[bodmas_index[x]]]:
bodmas_index.insert(x, i)
break
elif operator_precedence[calculation[i]] == operator_precedence[calculation[bodmas_index[x]]]:
if calculation[i] == “+” or calculation[i] == “-“:
bodmas_index.append(i)
break
else:
bodmas_index.insert(x + 1, i)
break
else:
continue
while len(bodmas_index) != 0:
# Loops through the operator indexes from left to right and performs the required operation, storing the result
# in calculation_result
if calculation[bodmas_index[0]] == ‘^’:
calculation_result = calculation[bodmas_index[0] 1] ** calculation[bodmas_index[0] + 1]
elif calculation[bodmas_index[0]] == ‘/’:
calculation_result = calculation[bodmas_index[0] 1] / calculation[bodmas_index[0] + 1]
elif calculation[bodmas_index[0]] == ‘*’:
calculation_result = calculation[bodmas_index[0] 1] * calculation[bodmas_index[0] + 1]
elif calculation[bodmas_index[0]] == ‘+’:
calculation_result = calculation[bodmas_index[0] 1] + calculation[bodmas_index[0] + 1]
else:
calculation_result = calculation[bodmas_index[0] 1] calculation[bodmas_index[0] + 1]
# calculation_result stores the result which is then inserted into the equation in place of the
# two values and operator that was used to calculate it.
calculation[bodmas_index[0]1] = calculation_result
calculation.pop(bodmas_index[0]+1)
calculation.pop(bodmas_index[0])
# Any operator indexes that are higher than the index stored at bodmas_index[0] will need to have their index position shifted by -2
# to accommodate for the shortening calculation. It’s been about 2 years since I wrote this code and I never commented on its behaviour,
# so here I am 2 years later trying to remember why I chose to do the loop this way.
print(bodmas_index)
for i in range(len(bodmas_index)):
if bodmas_index[i] > bodmas_index[0]:
bodmas_index.insert(i, bodmas_index[i] 2)
bodmas_index.pop(i + 1)
bodmas_index.pop(0)
return calculation[0]
def bracket_pair_finder(calculation):
# Pairs brackets together, so that the program knows which calculations to work out first
start_bracket_index_array = []
end_bracket_index_array = []
bracket_pairs = {}
for i in range(len(calculation)):
if calculation[i] == ‘(‘:
start_bracket_index_array.append(i)
elif calculation[i] == ‘)’:
end_bracket_index_array.append(i)
# Finds the innermost brackets so that they can be solved first
# Only returns one pair of brackets at a time.
# If all of them were returned, they could potentially be solved recursively, but I haven’t tried it
for i in range(len(start_bracket_index_array) 1, 1, 1):
for x in range(len(end_bracket_index_array)):
if end_bracket_index_array[x] < start_bracket_index_array[i] or end_bracket_index_array[x] in bracket_pairs.values():
continue
else:
bracket_pairs[start_bracket_index_array[i]] = end_bracket_index_array[x]
break
break
if len(bracket_pairs) != 0:
return bracket_pairs
def calculator(calculation):
brackets = bracket_pair_finder(calculation)
answer = []
if brackets is None:
return float(solve(calculation))
else:
start_bracket_index = list(brackets.keys())[0]
end_bracket_index = brackets[start_bracket_index]
answer.append(solve(calculation[start_bracket_index + 1:end_bracket_index]))
calculation = calculation[:start_bracket_index] + answer + calculation[end_bracket_index + 1:]
print(calculation)
return calculator(calculation)
def calc_input():
# Splits the calculation into an array of integers and BODMAS operators
calculation_array = []
calculation = input(‘Enter your calculation: ‘)
number = “”
for i in range(len(calculation)):
# If the current value is an int
if calculation[i].isnumeric() or calculation[i] == “.”:
# Because the equation is stored as a string, the numbers can be
# appended to (e.g. the string ’34’ can have ‘6’ appended to it, so it becomes ‘346’)
number = number + calculation[i]
# If the current iteration value is the last in the calculation, it can simply be appended,
# as there will be nothing left to append to the array after
if i == len(calculation)1:
calculation_array.append(float(number))
else:
#If the value being stored is not an int, it will be a BODMAS operator
# If there is no number currently being held in number, then the operator can be appended to the array
if number == “”:
calculation_array.append(calculation[i])
# Else, if there is a number currently being held in number, append it to the array and then append the operator after it
# Then reset the number variable to be empty, ready for the next lot of numbers to be stored in it.
else:
calculation_array.append(float(number))
calculation_array.append(calculation[i])
number = “”
return calculator(calculation_array)
answer = calc_input()
print(answer)

Yup that’s the code easy right. that’s it it doesn’t have numbers for lines cus the creator of this site doesn’t have time for adding those and coding to the article topics cus he’s just a dummy but i am sure he’ll soon till then hang on cus more codes will be incoming as I figure them out.  Bye Bye my Ducks

Advertisement

Top Badges To Earn

Platinum Cup

Won an official Social Network competition

Silver User

Has posted more than 100 posts on their profile

1 Required Step

  • Write an activity stream message 100 times

Gold User

Has posted more than 500 posts on their profile

1 Required Step

  • Write an activity stream message 500 times

Platinum User

Has posted more than 1000 posts on their profile

1 Required Step

  • Write an activity stream message 1000 times

Mightier Than Sword

Successfully published at least 10 blog posts

1 Required Step

  • Publish a new post 10 times

Caffeinated Readers

Received more than 500 daily visits on any post

1 Required Step

  • Daily visit any post 500 times

Advertisement

Advertisement

Advertisement

Advertisement

We would like to show you notifications for the latest news and updates.
Dismiss
Allow Notifications