Qasim-03 commited on
Commit
df0aef2
·
verified ·
1 Parent(s): e479561

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -21
app.py CHANGED
@@ -1,31 +1,52 @@
1
  import streamlit as st
2
 
3
  def main():
4
- st.title("Simple Calculator")
5
 
6
- # User input
7
- num1 = st.number_input("Enter first number", value=0.0, format="%.2f")
8
- num2 = st.number_input("Enter second number", value=0.0, format="%.2f")
9
 
10
- operation = st.selectbox("Choose an operation", ["Add", "Subtract", "Multiply", "Divide"])
 
 
 
11
 
12
- result = None
 
 
13
  if st.button("Calculate"):
14
- if operation == "Add":
15
- result = num1 + num2
16
- elif operation == "Subtract":
17
- result = num1 - num2
18
- elif operation == "Multiply":
19
- result = num1 * num2
20
- elif operation == "Divide":
21
- if num2 != 0:
22
- result = num1 / num2
23
- else:
24
- st.error("Cannot divide by zero!")
25
- result = None
26
-
27
- if result is not None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  st.success(f"Result: {result}")
 
 
29
 
30
  if __name__ == "__main__":
31
- main()
 
1
  import streamlit as st
2
 
3
  def main():
4
+ st.title("Advanced Calculator")
5
 
6
+ # Number of inputs
7
+ num_inputs = st.number_input("Enter the number of inputs", min_value=2, max_value=10, value=2, step=1)
 
8
 
9
+ # User inputs
10
+ numbers = []
11
+ for i in range(num_inputs):
12
+ numbers.append(st.number_input(f"Enter number {i+1}", value=0.0, format="%.2f"))
13
 
14
+ operation = st.selectbox("Choose an operation", ["Add", "Subtract", "Multiply", "Divide", "Modulus", "Exponentiation", "Floor Division"])
15
+
16
+ result = numbers[0]
17
  if st.button("Calculate"):
18
+ try:
19
+ for num in numbers[1:]:
20
+ if operation == "Add":
21
+ result += num
22
+ elif operation == "Subtract":
23
+ result -= num
24
+ elif operation == "Multiply":
25
+ result *= num
26
+ elif operation == "Divide":
27
+ if num != 0:
28
+ result /= num
29
+ else:
30
+ st.error("Cannot divide by zero!")
31
+ return
32
+ elif operation == "Modulus":
33
+ if num != 0:
34
+ result %= num
35
+ else:
36
+ st.error("Cannot find modulus with zero!")
37
+ return
38
+ elif operation == "Exponentiation":
39
+ result **= num
40
+ elif operation == "Floor Division":
41
+ if num != 0:
42
+ result //= num
43
+ else:
44
+ st.error("Cannot perform floor division by zero!")
45
+ return
46
+
47
  st.success(f"Result: {result}")
48
+ except Exception as e:
49
+ st.error(f"An error occurred: {e}")
50
 
51
  if __name__ == "__main__":
52
+ main()