AamerAkhter commited on
Commit
2a088e2
·
verified ·
1 Parent(s): e38d5ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -24
app.py CHANGED
@@ -1,44 +1,68 @@
1
  import streamlit as st
 
2
 
3
  def main():
4
- st.set_page_config(page_title="Simple Calculator", page_icon="🧮")
5
 
6
- st.title(" Simple Arithmetic Calculator")
7
- st.write("Enter two numbers and choose an operator to see the result.")
8
 
9
- # Layout using columns for a cleaner look
10
  col1, col2 = st.columns(2)
11
 
12
  with col1:
13
- num1 = st.number_input("Enter first number", value=0.0)
14
  with col2:
15
- num2 = st.number_input("Enter second number", value=0.0)
16
 
17
- # Operator selection
18
- operation = st.selectbox("Choose an operation", ("+", "-", "*", "/"))
 
 
 
19
 
20
- # Calculation logic
21
  result = None
22
  error_msg = None
23
 
24
  if st.button("Calculate"):
25
- if operation == "+":
26
- result = num1 + num2
27
- elif operation == "-":
28
- result = num1 - num2
29
- elif operation == "*":
30
- result = num1 * num2
31
- elif operation == "/":
32
- if num2 != 0:
33
- result = num1 / num2
34
- else:
35
- error_msg = "Cannot divide by zero!"
36
-
37
- # Display result
 
 
 
 
 
 
 
 
 
 
 
 
38
  if error_msg:
39
  st.error(error_msg)
40
- else:
41
- st.success(f"**Result:** {result}")
 
 
 
 
 
 
 
 
 
42
 
43
  if __name__ == "__main__":
44
  main()
 
1
  import streamlit as st
2
+ import math
3
 
4
  def main():
5
+ st.set_page_config(page_title="Advanced Calculator", page_icon="🔢")
6
 
7
+ st.title("🧮 Math Mastery Calculator")
8
+ st.write("Perform basic and advanced arithmetic operations.")
9
 
10
+ # Layout: Two columns for numbers
11
  col1, col2 = st.columns(2)
12
 
13
  with col1:
14
+ num1 = st.number_input("Enter first number (x)", value=0.0)
15
  with col2:
16
+ num2 = st.number_input("Enter second number (y)", value=0.0)
17
 
18
+ # Expanded operator selection
19
+ operation = st.selectbox(
20
+ "Choose an operation",
21
+ ("+", "-", "*", "/", "** (Power)", "% (Modulo)", "√ (Square Root of x)")
22
+ )
23
 
 
24
  result = None
25
  error_msg = None
26
 
27
  if st.button("Calculate"):
28
+ try:
29
+ if operation == "+":
30
+ result = num1 + num2
31
+ elif operation == "-":
32
+ result = num1 - num2
33
+ elif operation == "*":
34
+ result = num1 * num2
35
+ elif operation == "/":
36
+ if num2 != 0:
37
+ result = num1 / num2
38
+ else:
39
+ error_msg = "Division Error: You can't divide by zero!"
40
+ elif operation == "** (Power)":
41
+ result = math.pow(num1, num2)
42
+ elif operation == "% (Modulo)":
43
+ result = num1 % num2
44
+ elif operation == "√ (Square Root of x)":
45
+ if num1 >= 0:
46
+ result = math.sqrt(num1)
47
+ else:
48
+ error_msg = "Math Error: Cannot calculate square root of a negative number!"
49
+ except Exception as e:
50
+ error_msg = f"An error occurred: {e}"
51
+
52
+ # Display results with nice formatting
53
  if error_msg:
54
  st.error(error_msg)
55
+ elif result is not None:
56
+ st.balloons() # Just for a bit of fun on success
57
+ st.success(f"### Result: {result}")
58
+
59
+ # Adding a little tip section
60
+ with st.expander("Operation Help"):
61
+ st.write("""
62
+ * **Power**: Raises the first number to the power of the second ($x^y$).
63
+ * **Modulo**: Returns the remainder after division.
64
+ * **Square Root**: Only calculates the root of the **first** number provided.
65
+ """)
66
 
67
  if __name__ == "__main__":
68
  main()