Python_basic_5
Python_basic_5
Your Name
March 24, 2025
1 Introduction
This document presents a Python-based Simple Calculator. Users can perform
basic arithmetic operations including addition, subtraction, multiplication, di-
vision, and exponentiation.
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 power (a , b ) :
16 return a ** b
17
18 def main () :
19 print ( " Welcome to the Simple Calculator ! " )
20
21 while True :
22 print ( " \ n Calculator Menu : " )
23 print ( " 1 Addition (+) " )
24 print ( " 2 Subtraction ( -) " )
25 print ( " 3 Multi plicatio n (*) " )
26 print ( " 4 Division (/) " )
27 print ( " 5 Expon entiatio n (^) " )
28 print ( " 6 Exit " )
29
30 choice = input ( " Choose an option (1 -6) : " ) . strip ()
31
32 if choice in { " 1 " , " 2 " , " 3 " , " 4 " , " 5 " }:
1
33 try :
34 num1 = float ( input ( " Enter first number : " ) )
35 num2 = float ( input ( " Enter second number : " ) )
36
37 if choice == " 1 " :
38 print ( f " \ n Result : { num1 } + { num2 } = { add (
num1 , num2 ) }\ n " )
39 elif choice == " 2 " :
40 print ( f " \ n Result : { num1 } - { num2 } = {
subtract ( num1 , num2 ) }\ n " )
41 elif choice == " 3 " :
42 print ( f " \ n Result : { num1 } { num2 } = {
multiply ( num1 , num2 ) }\ n " )
43 elif choice == " 4 " :
44 print ( f " \ n Result : { num1 } { num2 } = {
divide ( num1 , num2 ) }\ n " )
45 elif choice == " 5 " :
46 print ( f " \ n Result : { num1 } ^ { num2 } = { power (
num1 , num2 ) }\ n " )
47 except ValueError :
48 print ( " Invalid input ! Please enter numbers only
.\ n " )
49 elif choice == " 6 " :
50 print ( " Exiting Calculator . Have a great day !\ n " )
51 break
52 else :
53 print ( " Invalid option ! Please try again .\ n " )
54
55 if __name__ == " __main__ " :
56 main ()