-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
51 lines (40 loc) · 1.31 KB
/
calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import streamlit as st
# Function to perform addition
def add(a, b):
return a + b
# Function to perform subtraction
def subtract(a, b):
return a - b
# Function to perform multiplication
def multiply(a, b):
return a * b
# Function to perform division
def divide(a, b):
if b == 0:
return "Error: Cannot divide by zero"
return a / b
# Calculator function
def calculator():
st.title("Simple Calculator")
st.markdown("### By Siddiqui Qamar") # Subheading
# Input fields for numbers
num1 = st.number_input("Enter the first number", step=1)
num2 = st.number_input("Enter the second number", step=1)
# Operation selection
operation = st.selectbox("Select operation", ["Addition", "Subtraction", "Multiplication", "Division"])
result = None
# Perform operation based on user input
if st.button("Calculate"):
if operation == "Addition":
result = add(num1, num2)
elif operation == "Subtraction":
result = subtract(num1, num2)
elif operation == "Multiplication":
result = multiply(num1, num2)
elif operation == "Division":
result = divide(num1, num2)
# Display result
if result is not None:
st.success(f"Result: {result}")
# Run the calculator function
calculator()