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

basic code 3

This document outlines a simple Python calculator program that performs basic arithmetic operations including addition, subtraction, multiplication, and division. The program features a menu-driven interface allowing users to select operations and handle input errors gracefully. It includes functions for each operation and a main loop to facilitate user interaction.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

basic code 3

This document outlines a simple Python calculator program that performs basic arithmetic operations including addition, subtraction, multiplication, and division. The program features a menu-driven interface allowing users to select operations and handle input errors gracefully. It includes functions for each operation and a main loop to facilitate user interaction.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Calculator Program

Your Name
March 24, 2025

1 Introduction
This document presents a simple calculator program written in Python. The
program allows users to perform basic arithmetic operations such as addition,
subtraction, multiplication, and division.

2 Python Code
1 def add (a , b ) :
2 return a + b
3
4 def subtract (a , b ) :
5 return a - b
6
7 def multiply (a , b ) :
8 return a * b
9
10 def divide (a , b ) :
11 if b == 0:
12 return " Error ! Division by zero . "
13 return a / b
14
15 def calculator () :
16 while True :
17 print ( " \ n Simple Calculator Menu : " )
18 print ( " 1 Add " )
19 print ( " 2 Subtract " )
20 print ( " 3 Multiply " )
21 print ( " 4 Divide " )
22 print ( " 5 Exit " )
23
24 choice = input ( " Choose an option (1 -5) : " ) . strip ()
25
26 if choice in [ " 1 " , " 2 " , " 3 " , " 4 " ]:
27 try :
28 num1 = float ( input ( " Enter first number : " ) )
29 num2 = float ( input ( " Enter second number : " ) )
30
31 if choice == " 1 " :

1
32 print ( f " Result : { num1 } + { num2 } = { add ( num1
, num2 ) } " )
33 elif choice == " 2 " :
34 print ( f " Result : { num1 } - { num2 } = { subtract
( num1 , num2 ) } " )
35 elif choice == " 3 " :
36 print ( f " Result : { num1 } * { num2 } = { multiply
( num1 , num2 ) } " )
37 elif choice == " 4 " :
38 print ( f " Result : { num1 } / { num2 } = { divide (
num1 , num2 ) } " )
39
40 except ValueError :
41 print ( " Invalid input ! Please enter numerical
values . " )
42
43 elif choice == " 5 " :
44 print ( " Exiting Calculator . Have a great day !\ n " )
45 break
46 else :
47 print ( " Invalid option ! Please try again . " )
48
49 if __name__ == " __main__ " :
50 calculator ()

You might also like