Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Task (Written in Python) Write the program calc.py which should impl

ID: 3740308 • Letter: P

Question

Programming Task (Written in Python)

Write the program calc.py which should implement a simple command-line calculator.

Requirements:

The program should accept three command line arguments: an operand, an operator, and another operand.

The program should check that the number and format of the arguments is correct. If there is an error with the arguments then the program should display an error message and exit.

The two operands are limited to valid integers; additionally, for the '^' operator the second operand must not be less than zero. The valid operators are '+' (addition), '-' (subtraction), 'x' (multiplication), '/' (integer division), '^' (exponentiation).

The program should display the result of the calculation and exit.

If the second operand to the division calculation is 0 then the program should not try to calculate the result but should display the word "undefined" and exit.

The program is not allowed to use Python's built-in '*', '/', '//', '%', and '**' operators, and it is not allowed to use the math library. The '+' and '-' operators are the only allowed arithmetic operators. The program is also allowed to use the compound assignment operators += and -=.

The multiplication, division, and exponentiation calculations must be implemented in three separate functions named times, divide, and power, respectively.

Explanation / Answer

import math
import random
# Define Variables
output = 0
num1 = ""
operator = ""
num2 = ""
memStore = "Empty"

# Define Function Listing Function
def abilitiesList():
print("+...Addition")
print("-...Subtract?ion")
print("*...Multiplication")
print("/...Division")
print("^...Powers")
  
  
def askForNumInput(textPrompt):
# Devine local variable
convertedNum = math.nan

# Wait for valid numerical input
while True:
num = input(textPrompt)
try:
# Try to typecast the input to a float
float(num)
except ValueError:
# Catch the exception if it is not a number
print("ERROR: Syn: Invalid Num")
else:
# Typecasting
convertedNum = float(num)
break
return convertedNum

# While Loop
while True:
print("//////////////////////////////////////////////////////////////////////////")
print("Type 'help' for a list of abilities")
# Loop for getting operation
while True:
operator = input("What operation do you want to perform? ")
# Is operator == to any of out constants or predefines?
if operator == "help":
abilitiesList()
  
elif operator == "MR":
print(str(memStore))
elif operator == "M-":
memStore = "Empty"
print("Memory Cleared")
elif operator == "rand":
print(random.random())
# Has the user entered in a valid operator?
elif operator == "+" or operator == "-" or operator == "*" or operator == "/" or operator == "^" or operator == "/-":
break
else:
print("ERROR: Invalid Operator")

# Loop for 1st number input
while True:
num1 = askForNumInput("First Number? ")
# Catch asin and acos out of bounds error case
if (operator == "asin" or operator == "acos") and (num1 > 1 or num1 < -1):
print("ERROR: Math: 'asin' and 'acos' commands only accept inputs in range -1 to +1")
elif operator == "!" and num1 < 0:
print("ERROR: Math: Factorials only accept inputs > 0")
else:
break

  
# Calculations
if operator == "+":
output = num1 + num2
print("Your Answer: "+str(output))
elif operator == "-":
output = num1 - num2
print("Your Answer: "+str(output))
elif operator == "*":
output = num1 * num2
print("Your Answer: "+str(output))
elif operator == "/":
output = num1 / num2
print("Your Answer: "+str(output))
elif operator == "^":
output = math.pow(num1,num2)
print("Your Answer: "+str(output))