CIS123_2.4_Using_Standard_Non-Standard_Python_Libraries_Implement_Functions_Corey_Adams

py

School

ECPI University, Greensboro *

*We aren’t endorsed by this school

Course

123

Subject

Computer Science

Date

Nov 24, 2024

Type

py

Pages

2

Uploaded by corada5995

Report
'''Create a program that will act as a calculator. It should be able to take two values from the keyboard, accept the operation to be performed \n" + and return the computed average. We will start simple here, assuming the user just enters two integers and will just request either addition, \n" + subtraction, division and multiplication.''' def setup(message): # Give the user directions for using this program. print(message) def getInfo(): number=input("Please enter your equation: ") try: if number.count(" ") == 2: #2 spaces means three words number1,operation,number2 = number.split(" ") number1 = int(number1) number2 = int(number2) else: number1,operation = number.split(" ") number1 = int(number1) number2="" return number1,number2,operation except: return 0,0,None from math import factorial def calculate(num1,num2,op): if (op== '+'): answer = num1 + num2 elif (op == '-'): answer = num1 - num2 elif (op== '/'): answer = num1 / num2 elif (op== "*"): answer = num1 * num2 elif (op=="//"): answer = num1 // num2 elif (op=="%"): answer = num1 % num2 elif (op=="**"): answer = pow(num1,num2) else: answer = None return answer def displayResult(num1,num2,op,answer): print(f'The answer to {num1} {op} {num2} = {answer}') def detailLoop(keepGoing): while keepGoing=="T": number1,number2,operation = getInfo() if operation is None: print("This equation was not in the correct format.") else:
result = calculate(number1,number2,operation) if (not result is None): displayResult(number1,number2,operation,result) else: print("We did not recognize the operation you requested") keepGoing = (input("Do you wish to perform another calculation? (T or F)")) def finishUp(message): print(message) WELCOME_MESSAGE='''Thank you for using our calculator - You will be prompted to enter an equation to solve: it should be entered on a single line in the format "a + b" - please be sure you leave at least one space between the operands and the operators. We have also added additional functionality to the calculator. You now have the following operations available: \n "+" addition "-" subtraction "/" real division "//" integer division - also call floor division in python "%" modulo "a ** b (take the first operand to the power of the second operand''' THANK_YOU_MESSAGE = '''Thank you for using our calculator''' def main(): keepGoing = "T" setup(WELCOME_MESSAGE) detailLoop=(keepGoing) finishUp(THANK_YOU_MESSAGE) # 2.x way # if _name_= "_main_": # main() main()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help