-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
62 lines (54 loc) · 1.98 KB
/
app.py
File metadata and controls
62 lines (54 loc) · 1.98 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def calculator():
"""
A simple calculator function that allows the user to perform basic arithmetic operations.
The user can select from the following operations:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Percentage
The function prompts the user to input their choice of operation and the required numbers.
It performs the selected operation and displays the result.
Error Handling:
- Ensures that the user inputs valid numbers.
- Handles division by zero gracefully by displaying an error message.
- Validates the user's choice of operation and prompts for a valid input if necessary.
Returns:
None
"""
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Percentage")
choice = input("Enter choice (1/2/3/4/5): ")
if choice in ['1', '2', '3', '4', '5']:
try:
num1 = float(input("Enter first number: "))
except ValueError:
print("Error: Please enter a valid number.")
try:
num2 = float(input("Enter second number: "))
except ValueError:
print("Error: Please enter a valid number.")
return
if choice == '5':
print(f"The result is: {num1 / 100}")
else:
num2 = float(input("Enter second number: "))
if choice == '1':
print(f"The result is: {num1 + num2}")
elif choice == '2':
print(f"The result is: {num1 - num2}")
elif choice == '3':
print(f"The result is: {num1 * num2}")
elif choice == '4':
if num2 != 0:
print(f"The result is: {num1 / num2}")
else:
print("Error: Division by zero is not allowed.")
else:
print("Invalid input. Please select a valid operation.")
if __name__ == "__main__":
calculator()