Ai Project
Ai Project
1|Page
INDEX
Contents Page no.
Acknowledgement…………………….03
Introduction………………………………04
Making Calculator
Using Python………………..….……….05
Step:1-: Create a File for the
Calculator……………………….05
Step 2: Prompt for User Input………05
Step 3: Perform Operations………….07
Step 4: Add Conditions…………………09
Step 5: Create Functions………………11
Acknowledgement -
2|Page
We are very thankful to everyone who all supported
us,we have completed our project effectively and
moreover on time. We are over helmed in all
humbleness and gratefulness to acknowledge our
depth to all those who have helped us to put these
ideas well.
We are equally grateful to our AI mam. She gave us
moral support and guided us in different matters
regarding the topic.
With the help of her valuable suggestions, guidance
and encouragement, we were able to perform this
project work. Last but not the least we would like to
thank our parents who helped us a lot in gathering
different information, collecting data and guiding us
from time to time in making this project despite of their
busy schedules, they gave us different ideas in making
this project unique.
Thanking you……
Preeti Prajnya Raya.
Tamralipta Jena
3|Page
Introduction-
What is Python?
Python is basically a high-level programming language.
It supports both object-oriented programming as well
as procedure-oriented programming. It is high-level
programming language with dynamic semantics
developed by Guido van Rossum. It was originally
released in 1991. Designed to be easy as well as fun,
the name "Python" is a nod to the British comedy group
Monty Python.
What is calculator?
A calculator is a device that performs arithmetic
operations on numbers. Basic calculators can do only
addition, subtraction, multiplication and division
mathematical calculations.
Directory creation.
File creation.
File editing in a text editor.
Start by creating a project directory and a file for the calculator code. On
Linux, follow the steps below:
mkdir Calculator
cd Calculator
4. Create a file with a .py extension and open it with a text editor, such
as nano:
nano calculator.py
The program enables the user to enter two numbers for a simple
calculation. Do the following:
5|Page
1. Fetch a user's input with Python's built-in input() method and save the
entry into two variables. Add the following code to the calculator.py file
you opened in the previous step:
The input() method accepts any entry type and saves the entered
information as a string.
In both cases, the program throws an error if the user enters anything that
is not a number.
3. Save the file and close the editor (for nano, use CTRL+X, confirm
with Y, and hit Enter).
6|Page
python3 calculator.py
Test multiple times to see the behavior for different user entries.
Mathematical operators.
String appending.
Operato
Description
r
+ Addition
- Subtraction
* Multiplication
** Power (exponent)
/ Division
// Floor division
7|Page
nano calculator.py
2. Add the following code to the file to print the result of different
operations:
# Perform operations
print("Sum: {} + {} = {}".format(a,b,a+b))
print("Difference: {} - {} = {}".format(a,b,a-b))
print("Product: {} * {} = {}".format(a,b,a*b))
print("Quotient: {} / {} = {}".format(a,b,a/b))
print("Power: {}^{} = {}".format(a,b,a**b))
print("Division with remainder: {} / {} = {} Remainder:
{}".format(a,b,a//b,a%b))
The program takes the two input numbers and prints the result of
different calculations using string concatenation.
python3 calculator.py
8|Page
Enter any two numbers to see the result of all the operations.
Multiline printing.
Conditional statements.
Catching errors with try except blocks.
print("""
Choose an operation from the list:
1. Addition
2. Subtraction
3. Multiplication
4. Exponentiation
5. Division
6. Division with remainder
""")
if op == 1:
print("Sum: {} + {} = {}".format(a,b,a+b))
elif op == 2:
9|Page
print("Difference: {} - {} = {}".format(a,b,a-b))
elif op == 3:
print("Product: {} * {} = {}".format(a,b,a*b))
elif op == 4:
print("Power: {}^{} = {}".format(a,b,a**b))
elif op == 5:
try:
print("Quotient: {} / {} = {}".format(a,b,a/b))
except:
print("Division by 0 not possible!")
elif op == 6:
try:
print("Division with remainder: {} / {} = {} Remainder:
{}".format(a,b,a//b,a%b))
except:
print("Divsion by 0 not possible!")
else:
print("No such choice!")
The code adds new features and functionalities. Below is a brief overview:
The first part of the code generates a simulated user menu with
choices (multiline comments). The program saves the user input
into variables (first number, second number, and operation).
The second part of the code takes the user input variables and
performs a calculation based on the input. The final else block
prints a message if a user chooses something that's not an
option in the program.
In the case of division by zero, the program uses a try
except block to catch the program error and prints a descriptive
error message.
10 | P a g e
Save and run the code to see how the program works:
python3 calculator.py
Run the code several times for different user inputs to see how the output
and behavior differ.
Separate the code into logical units and use a recursive function to loop
the program. Modify the code in the calculator.py file to match the
following:
def prompt_menu():
a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
print("""
Choose an operation from the list:
1. Addition
2. Subtraction
3. Multiplication
4. Exponentiation
5. Division
6. Division with remainder
""")
11 | P a g e
op = int(input("Enter the choice number: "))
return a, b, op
def calculate():
a, b, op = prompt_menu()
if op == 1:
print("Sum: {} + {} = {}".format(a,b,a+b))
elif op == 2:
print("Difference: {} - {} = {}".format(a,b,a-b))
elif op == 3:
print("Product: {} * {} = {}".format(a,b,a*b))
elif op == 4:
print("Power: {}^{} = {}".format(a,b,a**b))
elif op == 5:
try:
print("Quotient: {} / {} = {}".format(a,b,a/b))
except:
print("Division by 0 not possible!")
elif op == 6:
try:
print("Division with remainder: {} / {} = {} Remainder:
{}".format(a,b,a//b,a%b))
except:
print("Divsion by 0 not possible!")
else:
print("No such choice!")
loop()
def loop():
choice = input("Do you want to continue? (Y,N): ")
if choice.upper() == "Y":
calculate()
elif choice.upper() == "N":
print("Goodbye!")
else:
print("Invalid input!")
loop()
calculate()
The prompt_menu() function contains the user menu and returns the
two input numbers and selected operation.
The calculate() function uses the prompt_menu() function to gather
the user input and calculate based on the provided information.
The loop() function creates a loop menu where the user chooses
whether to continue using the program. In case of invalid input, the
12 | P a g e
function recursively calls itself and reruns the function. The final line
in the calculate() function calls the loop() function.
After the function definitions, the program calls the calculate() function to
run the program loop.
Save the code and run the program with the following:
python3 calculator.py
The program loops until the user enters N or n to exit the program.
Conclusion
After working through the steps in this guide, wehave a fully-functional
calculator program written in Python. Improving the existing code or
taking a completely different approach is possible.
13 | P a g e