Quantcast
Channel: Calculator final project in Python - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 4

Calculator final project in Python

0
0

In my final project for my Python course, I am required to create a calculator program. I really need someone to look over my code and tell me if I have met all the requirements for this project.

Here are the requirements:

For the final project, you are to write a calculator program in Python with the following characteristics:

  • It must be at least 50 lines of code excluding comments
  • It must have the following functions:+,-,*,/, SQRT, and work with decimal numbers.
  • It must be all text, no high resolution graphics
  • It must automatically copy the result to the operating system clipboard
  • It must work as a functional calculator (calculations can be entered over and over)

Please submit the source code only (.py), no object code (I will compile on receipt)

NOTE:

  1. This assignment is graded on a pass/fail basis; there is no partial credit or resubmissions. Be sure you understand ALL of the requirements.

  2. Solutions that require the user install any software will NOT be accepted (in case you are not sure, this means NO Pyperclip.)

WARNING: All submissions will be checked for plagiarism using SafeAssign or other plagiarism checker.

I completed the project, but because this is a pass/fail project I want to be 200% sure I have met every requirement. I am also open to any other feedback that would sharpen my Python skills.

Menu

def main():
    #Menu
    print("=======================")
    print("Welcome to Calculator")
    print("By: Tyler Harris")
    print("=======================")
    print("Menu: ")
    print("[1] Calculator")
    print("[2] Instructions")
    print("[3] Exit")
    print("=======================")
    choice = input("Please select an option: ")
    if choice == '1':
        calculator()
    elif choice == '2':
        instructions()
        main()
    elif choice == '3':
        print("Thank you for using Calculator.")
    else:
        print("Not an option, try again:")
        main()

Calculator

def calculator():
    result=0        #Resets calulator.
    try:
        while True:
            print("Last Result: "+str(result))

            #Number 1 input.
            if result == 0:
                num1= int(input("First Number: "))
            if result != 0:     
                num1=result

            #Operator input.
            #Checks for input of special operators 'c', 'sqrt', and 'esc'.
            operator=input("Opertaor: ").lower()
            if operator == "c":
                print("Calculator cleared")
                calculator()
            if operator=="sqrt":
                result=(sqrt(num1))
                print("Result: "+str(result))
                continue
            if operator=="esc":
                main()
            if operator.isdigit():      #Throw error if operator is a number.
                raise Exception()

            #Number 2 input.    
            num2= int(input("Second Number: "))
            #Operator calls.
            if operator=="+":
                result=(add(num1,num2))
            elif operator=="-":
                result=(sub(num1,num2))
            elif operator=="*":
                result=(mult(num1,num2))
            elif operator=="/":
                result=(div(num1,num2))


            #Copy result to System's clipboard and display the result.    
            copy2clip(str(result))      
            print("=======================")
            print("Result: "+str(result))
            print("copied to clipboard")
            print("=======================")

    #Catch any errors and reset calculator            
    except Exception:
        print("=======================")
        print("Error Please try again.")
        calculator()
    else:
        calculator()

Operators

def add(num1,num2):
    return num1 + num2
def sub(num1,num2):
    return num1 - num2
def mult(num1,num2):
    return num1 * num2
def div(num1,num2):
    return num1 / num2
def sqrt(num1):
    return (num1**(1/2.0))

Instructions

def instructions():
    print("=======================")
    print(format('Calculator','^25s'))
    print("=======================")
    print("Available operators")
    print("+: Addition")
    print("-: Subtraction")
    print("*: Multiplication")
    print("/: Division")
    print("sqrt: Square Root")
    print("c: Clear Calculator")
    print("esc: Exit Calculator")
    print("=======================")

def copy2clip(txt):
    cmd='echo '+txt.strip()+'|clip'
    return subprocess.check_call(cmd, shell=True)

if __name__=='__main__':
    main()

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images