AIRaja commited on
Commit
f46132d
·
verified ·
1 Parent(s): 3fb2650

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -18
app.py CHANGED
@@ -1,32 +1,65 @@
1
  import streamlit as st
2
 
3
  def main():
4
- st.title("Simple Calculator")
5
 
6
- # Input fields for numbers
7
  num1 = st.number_input("Enter the first number", value=0.0)
8
  num2 = st.number_input("Enter the second number", value=0.0)
 
9
 
10
  # Dropdown for selecting the operator
11
- operator = st.selectbox("Select an operation", ["+", "-", "*", "/"])
 
 
 
 
 
 
 
 
 
12
 
13
  if st.button("Calculate"):
14
  result = None
15
- # Perform calculation based on the selected operator
16
- if operator == "+":
17
- result = num1 + num2
18
- elif operator == "-":
19
- result = num1 - num2
20
- elif operator == "*":
21
- result = num1 * num2
22
- elif operator == "/":
23
- if num2 == 0:
24
- st.error("Error: Division by zero is not allowed.")
25
- else:
26
- result = num1 / num2
27
-
28
- if result is not None:
29
- st.success(f"Result: {result}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  if __name__ == '__main__':
32
  main()
 
1
  import streamlit as st
2
 
3
  def main():
4
+ st.title("Enhanced Calculator")
5
 
6
+ # Input fields for three numbers
7
  num1 = st.number_input("Enter the first number", value=0.0)
8
  num2 = st.number_input("Enter the second number", value=0.0)
9
+ num3 = st.number_input("Enter the third number", value=0.0)
10
 
11
  # Dropdown for selecting the operator
12
+ operations = [
13
+ "Addition",
14
+ "Subtraction",
15
+ "Multiplication",
16
+ "Division",
17
+ "Modulus",
18
+ "Exponentiation",
19
+ "Average"
20
+ ]
21
+ operator = st.selectbox("Select an operation", operations)
22
 
23
  if st.button("Calculate"):
24
  result = None
25
+
26
+ try:
27
+ if operator == "Addition":
28
+ result = num1 + num2 + num3
29
+
30
+ elif operator == "Subtraction":
31
+ result = num1 - num2 - num3
32
+
33
+ elif operator == "Multiplication":
34
+ result = num1 * num2 * num3
35
+
36
+ elif operator == "Division":
37
+ # Check for division by zero
38
+ if num2 == 0 or num3 == 0:
39
+ st.error("Error: Division by zero is not allowed.")
40
+ else:
41
+ result = num1 / num2 / num3
42
+
43
+ elif operator == "Modulus":
44
+ # Check for modulus by zero
45
+ if num2 == 0 or num3 == 0:
46
+ st.error("Error: Modulus by zero is not allowed.")
47
+ else:
48
+ result = (num1 % num2) % num3
49
+
50
+ elif operator == "Exponentiation":
51
+ # We'll calculate as (num1 ** num2) ** num3
52
+ result = (num1 ** num2) ** num3
53
+
54
+ elif operator == "Average":
55
+ result = (num1 + num2 + num3) / 3
56
+
57
+ # Display the result if it was computed
58
+ if result is not None:
59
+ st.success(f"Result: {result}")
60
+
61
+ except Exception as e:
62
+ st.error(f"An error occurred: {e}")
63
 
64
  if __name__ == '__main__':
65
  main()