0% found this document useful (0 votes)
17 views

Ai Project

.

Uploaded by

preetiraya1902
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Ai Project

.

Uploaded by

preetiraya1902
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Certificate-

This is to certify that Preeti Prajnya Raya &


Tamralipta Jena of class 10th B have successfully
completed their project on topic Design Of
Simple Calculator Using Python as prescribed by
AI Mam during the academic year 2023-24 as per
the guidelines given by CBSE.

Teacher’s signature Principal’s


signature

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.

Making calculator using Python


How?
4|Page
Step 1: Create a File for the
Calculator
The first step covers the following skills:

 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:

1. Open the terminal (CTRL+Alt+T).

2. Create the project directory with the mkdir command:

mkdir Calculator

3. Change the directory with the following:

cd Calculator

4. Create a file with a .py extension and open it with a text editor, such
as nano:

 nano calculator.py

Keep the file open and proceed to the next step.

Step 2: Prompt for User Input


The second step covers the following:

 Writing comments in Python.


 Taking user input.
 Converting user input to a desired data type.
 Saving and running the code.

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:

# Prompt for user input

a = input("Enter the first number: ")


b = input("Enter the second number: ")

The input() method accepts any entry type and saves the entered
information as a string.

2. Limit user entry to numbers. If performing calculations with whole


numbers (integer calculations), encase the input() method in int() to
convert the input into an integer:

# Prompt for user input

a = int(input("Enter the first number: "))


b = int(input("Enter the second number: "))

The better option is to use float() to perform more precise calculations.


To allow decimal calculations, convert the user entry into floating point
numbers:

# Prompt for user input

a = float(input("Enter the first number: "))


b = float(input("Enter the second number: "))

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).

4. Run the program to see how it works:

6|Page
python3 calculator.py

Test multiple times to see the behavior for different user entries.

Step 3: Perform Operations


The third step covers the following concepts:

 Mathematical operators.
 String appending.

Operato
Description
r

+ Addition

- Subtraction

* Multiplication

** Power (exponent)

/ Division

// Floor division

% Modulo (division remainder)

Decide what kind of operations the calculator performs. Below is a brief


table with the available built-in operators in Python.

To create and test different operations:

1. Open the calculator.py file again:

7|Page
nano calculator.py

2. Add the following code to the file to print the result of different
operations:

# Prompt for user input

a = float(input("Enter the first number: "))


b = float(input("Enter the second number: "))

# 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.

3. Save and close the file.

4. Run the program to test:

python3 calculator.py

8|Page
Enter any two numbers to see the result of all the operations.

Step 4: Add Conditions


The fourth step covers these functionalities:

 Multiline printing.
 Conditional statements.
 Catching errors with try except blocks.

Conditional statements in Python help control the program flow based on


a value. Instead of performing all operations on the two input numbers,
allow users to choose and check the input using a conditional statement.

A multiline comment enables a quick way to create a menu with choices.

Change the code in the calculator.py file to match the following:

# First part: Prompt for user input

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
""")

op = int(input("Enter the choice number: "))

# Second part: Perform operations based on input

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.

Step 5: Create Functions


The fifth step in the calculator program covers the following:

 Separating code into functions.


 Looping the program.

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 code has three distinct functions:

 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

You might also like