SOURCE CODE
'''Importing turtle module for advanced use and advanced features like exceptional handling etc. modified by me(Mahad).'''
import turtle
# Making circle in grey color with white background
turtle.dot(550,"gray")
turtle.showturtle()
turtle.goto(-180,0)
turtle.pencolor("white")
turtle.pensize(10)
turtle.penup()
# Writing instructions
turtle.write("Welcome to our calculator\n\nInstructions to use simple calculator:\n1. Please enter user name\n2. Please enter first number \
\n3. Please enter second number\n\
4. Choose appropriate operation to add,multiply,subtract etc from instructions \
payment\n5. Get your desired result\n6. Enter 'Y' to use again or any other key to exit\nNote:\n1. You have to enter numbers only otherwise it will show invalid input.\n2. It will give Math error if any number divisible by 0 \nThanks for using our system\n\nA simple calculator programmed by M.Mahad Rasool")
# Hiding turtle sign for making more stylish theme
turtle.hideturtle()
# Writing welcome function to welcome user
def welcome():
print("-------Welcome to our online calculation system----------")
# Using strip() function to remove whitespace characters
name = input("Please enter your name: ").strip()
print("Hello",name,"\nLet's use calculator")
# Calling welcome() function to print welcome statement
welcome()
# Importing math function to use in calculaton
import math
import time
# Instructions for user to know about calculator operations
print("\nPlease choose math operation you want from below operations:\n\
'+' for add \n'-' for substraction\n\
'/' for division\n'*' for multiplication\n'^' for finding \
power of first number with second number as power\n'log' to find logarithm of both functions\n'sin' for finding sin of both numbers\n\
'cos' for finding sin of both numbers\n'tan' for finding sin of both numbers")
# Writng main function for which the calculator operation depends
def calculation(number1,number2):
# Ask user to enter add,substraction or other function by #choosing symbols from instructions
# Using strip() function to remove whitespace characters
function = input("Enter operation: ").strip()
# Adding additional function of 'Math Error' if any number is #divided by 0
if number2 == 0 and function == '/' :
print("Math Error")
improve()
if function == '+':
print(number1,'+',number2,'=')
print(number1 + number2)
elif function == '-':
print(number1,'-',number2,'=')
print(number1 - number2)
elif function == '/':
print(number1,'/',number2,'=')
print(number1 / number2)
elif function == '*':
print(number1,'*',number2,'=')
print(number1 * number2)
elif function == '^':
print(number1,'^',number2,'=')
print(number1 ^ number2)
elif function == 'log':
print("log",number1,'=',math.log(number1))
print("log",number2,'=',math.log(number2))
elif function == "sin":
print("sin",number1,'=',math.sin(number1))
print("sin",number2,'=',math.sin(number2))
elif function == 'cos':
print("cos",number1,'=',math.cos(number1))
print("cos",number2,'=',math.cos(number2))
elif function == 'tan':
print("tan",number1,'=',math.tan(number1))
print("tan",number2,'=',math.tan(number2))
else:
print("Invalid input")
# Calling improve function to choose for further use calculator #or not
improve()
# Make improve() function to allow user to further use calculator or #not
# Note that make improve() function below main calculator function #because\
# we have to use main calculator function in it to run again.
def improve():
print("\nPlease enter 'Y' for perform other functions or any other key to exit")
# Making user choice upper case
response = input("Enter your choice: ").upper()
if response == 'Y':
'''Taking input from user with different variable name. Note that first_name and first_name1 is different.
Taking input from other name is necessary to prevent from local and global variable names error'''
try:
first_number1 = eval(input("\nEnter your first number: "))
second_number2 = eval(input("Enter your second number: "))
except:
print("Sorry, try again.Alphabets not allowed and number must not start with zero.\nInvalid input")
improve()
#Again calling main calculator function with different input #variable
calculation(first_number1,second_number2)
else:
print("Thanks for using our system")
# importing 'sys' library to exit function.
# It is better way to use sys.exit() function as exit() kills #the function making it program not professional.
import sys
# Exiting function here because user wants to quit calculator
print("Program will terminate automatically after 5 seconds")
time.sleep(5)
sys.exit()
# Taking input from user and applying test function 'try/except' to #check input is numbers or not
try:
first_number = eval(input("\nEnter your first number: "))
second_number = eval(input("Enter your second number: "))
except:
print("Sorry, try again.Alphabets not allowed and number must not start with zero.\nInvalid input")
# If not correct input calling improve function to let user to #choose to use calculator again
improve()
# Calling the main calculator function when running first time
calculation(first_number,second_number)
1 Comments
When i tried running the program , it says "(", was never closed , and this bracket is in the # writing instructions
ReplyDelete