JaveriaZia commited on
Commit
09bc469
·
verified ·
1 Parent(s): 7ff399d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -21
app.py CHANGED
@@ -1,29 +1,77 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  # Title
4
- st.title("Simple Calculator App")
5
 
6
  # Input fields
7
- num1 = st.number_input("Enter first number", format="%.2f")
8
- num2 = st.number_input("Enter second number", format="%.2f")
 
 
 
 
 
 
9
 
10
- # Operation selection
11
- operation = st.selectbox("Select operation", ["Add", "Subtract", "Multiply", "Divide"])
12
 
13
- # Calculate and display result
14
  if st.button("Calculate"):
15
- if operation == "Add":
16
- result = num1 + num2
17
- st.success(f"Result: {result}")
18
- elif operation == "Subtract":
19
- result = num1 - num2
20
- st.success(f"Result: {result}")
21
- elif operation == "Multiply":
22
- result = num1 * num2
23
- st.success(f"Result: {result}")
24
- elif operation == "Divide":
25
- if num2 != 0:
26
- result = num1 / num2
27
- st.success(f"Result: {result}")
28
- else:
29
- st.error("Cannot divide by zero")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import math
3
+
4
+ # Custom styling using Markdown
5
+ st.markdown(
6
+ """
7
+ <style>
8
+ .main {
9
+ background-color: #f0f4f8;
10
+ }
11
+ .stButton>button {
12
+ color: white;
13
+ background-color: #4CAF50;
14
+ font-size: 16px;
15
+ border-radius: 10px;
16
+ padding: 10px 24px;
17
+ }
18
+ .stSelectbox, .stNumberInput {
19
+ background-color: #ffffff;
20
+ }
21
+ </style>
22
+ """,
23
+ unsafe_allow_html=True
24
+ )
25
 
26
  # Title
27
+ st.markdown("<h1 style='color: #3366cc; text-align: center;'>Advanced Calculator</h1>", unsafe_allow_html=True)
28
 
29
  # Input fields
30
+ num1 = st.number_input("Enter First Number", format="%.4f")
31
+ num2 = st.number_input("Enter Second Number", format="%.4f")
32
+
33
+ # Operation choices
34
+ operations = [
35
+ "Add", "Subtract", "Multiply", "Divide",
36
+ "Power", "Modulus", "Square Root (1st Number)", "Logarithm (1st Number)"
37
+ ]
38
 
39
+ operation = st.selectbox("Choose Operation", operations)
 
40
 
41
+ # Calculate button
42
  if st.button("Calculate"):
43
+ try:
44
+ if operation == "Add":
45
+ result = num1 + num2
46
+ elif operation == "Subtract":
47
+ result = num1 - num2
48
+ elif operation == "Multiply":
49
+ result = num1 * num2
50
+ elif operation == "Divide":
51
+ if num2 != 0:
52
+ result = num1 / num2
53
+ else:
54
+ st.error("❌ Cannot divide by zero!")
55
+ result = None
56
+ elif operation == "Power":
57
+ result = num1 ** num2
58
+ elif operation == "Modulus":
59
+ result = num1 % num2
60
+ elif operation == "Square Root (1st Number)":
61
+ if num1 >= 0:
62
+ result = math.sqrt(num1)
63
+ else:
64
+ st.error("❌ Cannot take square root of a negative number!")
65
+ result = None
66
+ elif operation == "Logarithm (1st Number)":
67
+ if num1 > 0:
68
+ result = math.log(num1)
69
+ else:
70
+ st.error("❌ Logarithm undefined for 0 or negative numbers!")
71
+ result = None
72
+
73
+ if result is not None:
74
+ st.success(f"✅ Result: {result:.4f}")
75
+
76
+ except Exception as e:
77
+ st.error(f"⚠️ Error: {str(e)}")