Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple calculator
#1
import math

def allString(first, second):

  operation = ('add', 'subtract', 'multiply', 'divide')
  for v in operation:
    print(f"you cannot {v} {first} to {second}")

def getBoolean(first, second):
  operands = []
  # converting this to upper so checking will be minimal
  if (first.upper() == 'TRUE'):
    operands.append(True)
  if (first.upper() == 'FALSE'):
    operands.append(False)
  if (second.upper() == 'TRUE'):
    operands.append(True)
  if (second.upper() == 'FALSE'):
    operands.append(False)
  
  return operands

def evaluateBoolean(first, second):
  operands = getBoolean(first, second)

  if operands[0] and operands[1]:
    andValue = True
  else:
    andValue = False

  if operands[0] or operands[1]:
    orValue = True
  else:
    orValue = False

  print("\nSelection = G (Logical and)")
  print(f"{first} and {second} is {andValue}\n")
  print("Selection = H (Logical or)")
  print(f"{first} or {second} is {orValue}\n")

def convertOperands(first_operand, second_operand):
  operands = []
  operands.append(int(first_operand))
  operands.append(int(second_operand))
  return operands

def evaluateExpression (first_operand, second_operand):
  operands = convertOperands(first_operand, second_operand)

  sum = operands[0] + operands[1]
  difference = operands[0] - operands[1]
  product = operands[0] * operands[1]
  quotient = operands[0] / operands[1]
  whole = math.floor(quotient)
  remain = operands[0] % operands[1]
  power = math.pow(operands[0], operands[1])

  print("\nSelection = A (add)")
  print(f"The sum of {operands[0]} and {operands[1]} is: {sum}\n")
  print("Selection = B (subtract)")
  print(f"The difference between {operands[0]} and {operands[1]} is: {difference}\n")
  print("Selection = C (multiplicaton)")
  print(f"The product of {operands[0]} and {operands[1]} is: {product}\n")


  choice = input("Do you want to display decimals?: ")
  
  if (choice.upper() == 'YES' or 'Y'):
    print("\nSelection = D (division)")
    print(f"The complete quotient between {operands[0]} and {operands[1]} is: {quotient}\n")
  else:
    print("\nSelection = D (division)")
    print(f"The rounded quotient between {operands[0]} and {operands[1]} is: {whole}\n")

  
  print("Selection = E (Modulo)")
  print(f"The remainder of {operands[0]} divided by {operands[1]} is: {remain}\n")
  print("Selection = F (Exponent)")
  print(f"{operands[0]} raised to the power of {operands[1]} is: {power}\n")


def checkInput():
  user_input = input("Input: ")
  operands = user_input.split('&')
   

  if '"' in operands[0] or '"' in operands[1] or "'" in operands[0] or "'" in operands[1]:
    allString(operands[0], operands[1])
  # detect if there are boolean inputs
  elif 'true' in operands[0] or 'false' in operands[0]:
    evaluateBoolean(operands[0], operands[1])
  # if none of the above, we evaluate input as digits
  else:
    evaluateExpression(operands[0], operands[1])

checkInput()
how do i change/add selection as an input??
Reply
#2
(Sep-21-2022, 11:02 AM)ryza Wrote: how do i change/add selection as an input??
?? I don't understand your question.

What do you want to change?
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
Can someone teach me how to code this

Attached Files

Thumbnail(s)
   
Reply
#4
Don't double post. You already posted this question in the homework section.

This is not a forum for having people do your homework.

Maybe start by taking input and splitting into two values. You might need to strip any whitespace around the values. Test with different inputs to make sure the input is processed correctly.

Come up with a way to determine if the values are numeric. There are multiple ways to determine if a str is a number. It is easy to find that information on the internet. Get the code working and test with multiple inputs.

Come up with a way to determine if the values are boolean. What are valid boolean values? Does it have to be True and False or can it be T and F? It is your calculator, so you get to decide. Once your code works, test with multiple inputs.

If the input is not a boolean or a number does that mean it is a string? Or do does a string have to be inside surrounded by quotes? If the latter, you'll need to come up with a way to identify if the input is a string. Once you have it working test with multiple inputs.

Once you identify the value types. You'll need to print a message to the user asking to input an operator. The directions have a list of operators, and you should present this information to the user. Then you have to take input and check if it is one of the choices. I would first write this as an independent program so you don't have to go through the hassle of entering values just to test entering the operator. Test with multiple inputs to verify it works then integrate into your program. And then test again.

When you get that working you only have to perform the operation and print the result.

Just do it one step at a time. One step at a time keeps each task small and contained. Testing after each step means that you can focus on the next step and not have to worry if new problems are because of the latest step or code from a previous step. Thinking about the entire problem will make you feel overwhelmed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Distance Between Points Calculator Spade 1 1,711 Sep-17-2021, 03:14 PM
Last Post: DeaD_EyE
  Printing simple calculator answer LCFC2020 2 2,153 Dec-05-2020, 02:07 PM
Last Post: MK_CodingSpace
  simple calculator shakeelthomas 11 6,043 Jul-23-2018, 10:47 PM
Last Post: shakeelthomas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020