Demo entry 6691689

Calculator

   

Submitted by David Feng on Jan 12, 2018 at 22:13
Language: Python 3. Code size: 2.3 kB.

import math

#texts to display
print("┌----------------------------------------------------------┐")
print("| Welcome to Text-based Calculator version 3.0 by David Feng |")
print("└----------------------------------------------------------┘")
print("")
print("Functions:")
print("[+] number1 plus number2")
print("[-] number1 subtract number2")
print("[*] number1 multiply number2")
print("[/] number1 divide number2")
print("[^] number1 to the power of number2")
print("[sin] the sine of number")
print("[cos] the cosine of number")
print("[tan] the tangent of number")
print("[m] change memory number")
print("")

while(1 == 1):
#asking questions
    func = input("Please input function.")
    if(func == "m"):
        mem = float(input("Please enter new value of m."))
        print("Memory number changed to", mem)
    elif((func == "sin") or (func == "cos") or (func == "tan")):
        num1str = str(input("Please enter the number."))
        if(num1str == "m"):
            num1 = mem
        else:
            num1 = float(num1str)
    else:
        num1str = str(input("Please enter the first number."))
        num2str = str(input("Please enter the second number."))
        if(num1str == "m"):
            num1 = mem
        else:
            num1 = float(num1str)
        if(num2str == "m"):
            num2 = mem
        else:
            num2 = float(num2str)

    

#calculation process
    if(func == "+"):
        print("The answer is",num1 + num2)
        mem = (num1 + num2)
    elif(func == "-"):
        print("The answer is",num1 - num2)
        mem = (num1 - num2)
    elif(func == "*"):
        print("The answer is",num1 * num2)
        mem = (num1 * num2)
    elif(func == "/"):
        print("The answer is",num1 / num2)
        mem = (num1 / num2)
    elif(func == "^"):
        print("The answer is",num1 ** num2)
        mem = (num1 ** num2)
    elif(func == "sin"):
        print("The answer is",math.sin(num1))
        mem = (math.sin(num1))
    elif(func == "cos"):
        print("The answer is",math.cos(num1))
        mem = (math.cos(num1))
    elif(func == "tan"):
        print("The answer is",math.tan(num1))
        mem = (math.tan(num1))
    print("")

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).