-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
47 lines (31 loc) · 881 Bytes
/
calculator.py
File metadata and controls
47 lines (31 loc) · 881 Bytes
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
import streamlit as st
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
raise ValueError("Cannot divide by zero.")
return a / b
st.title("🧮 Calculator")
# Inputs
num1 = st.number_input("Enter first number", value=0.0)
num2 = st.number_input("Enter second number", value=0.0)
st.write("### Choose Operation")
col1, col2, col3, col4 = st.columns(4)
result = None
try:
if col1.button("➕ Add"):
result = add(num1, num2)
if col2.button("➖ Subtract"):
result = subtract(num1, num2)
if col3.button("✖ Multiply"):
result = multiply(num1, num2)
if col4.button("➗ Divide"):
result = divide(num1, num2)
if result is not None:
st.success(f"Result: {result}")
except ValueError as e:
st.error(str(e))