In: Computer Science
Create a program using python that provides a simple calculator:
Requires a login with a prompt for a username and a password prior to using the calculator
If username and password are incorrect, the program should re-prompt to re-enter correct username and password
Once logged in, the user should have access to the calculator and should have the ability for the following mathematical operators:
Addition Subtraction
Multiplication
Division
Square Root
PI
Exponents
# to use sqrt, pi bulit in fucntion
import math
# fucntion to calculate addition
def addition(num1, num2):
return num1 + num2
# fucntion to calculate subtraction
def subtraction(num1, num2):
return num1 - num2
# fucntion to calculate multiplication
def multiplication(num1, num2):
return num1 * num2
# fucntion to calculate division
def division(num1, num2):
return num1/ num2
# fucntion to calculate square root
def squareRoot(num):
return math.sqrt(num)
# fucntion return the value of PI
def pi():
return math.pi
# fucntion to calculate exponent
def exponent(num):
return math.exp(num)
# fucntion show to options to user
def options():
print("------Calculator-----")
print("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Square Root\n6.PI\n7.Exponent")
choice = int(input("Enter your choice: "))
return choice
# fucntion to print the result
def printResult(result):
print("Result: " + str(result))
# main fucntion
def main():
username = input("Enter the username: ")
password = input("Enter the password: ")
# lets username is 'username' and password is 'password'
# checking user name and password
if username == 'username' and password == 'password':
# getting user's choice
choice = options()
# checking user choice
if choice == 1:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = addition(num1, num2)
# printing the reslult
printResult(result)
elif choice == 2:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = subtraction(num1, num2)
# printing the reslult
printResult(result)
elif choice == 3:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = multiplication(num1, num2)
# printing the reslult
printResult(result)
elif choice == 4:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = division(num1, num2)
# printing the reslult
printResult(result)
elif choice == 5:
num = float(input("Enter the number: "))
result = squareRoot(num)
# printing the reslult
printResult(result)
elif choice == 6:
print("Value of PI is: " + str(pi()))
elif choice == 7:
num = float(input("Enter the number: "))
result = exponent(num)
# printing the reslult
printResult(result)
else:
print("Wrong option selected :(:( ")
else:
print("Incorrect username or password :(:( ")
if __name__ == "__main__":
# calling the main function
main()
Note: Let me know if you have any doubt..