-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHWCalculator.py
More file actions
57 lines (48 loc) · 1.92 KB
/
HWCalculator.py
File metadata and controls
57 lines (48 loc) · 1.92 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
# Task: Create a Calculator
# Your task is to create a basic calculator program using Python.
# The program should allow the user to perform simple arithmetic operations on two numbers.
# Requirements:
# Prompt the user to enter two numbers.
# Prompt the user to select an operation from the following options:
# addition
# subtraction
# multiplication
# division.
# Based on the selected operation, perform the corresponding calculation. Display the result to the user.
# Note:
# Ensure that the program handles division by zero and provides an appropriate error message if the user attempts to divide by zero.
# Consider using functions to encapsulate the calculation logic for each operation.
# Include clear instructions and error handling for invalid input.
print("Welcome to the Calculator Program!")
next_operation = 'y'
while next_operation == 'y':
while True:
number1 = input("Please enter the first number: ")
try:
number1 = float(number1)
break
except ValueError:
print("Error. Please use numbers only")
print()
while True:
number2 = input("Please enter the second number: ")
try:
number2 = float(number2)
break
except ValueError:
print("Error. Please use numbers only")
print()
operation = input("Please select an operation: +, -, *, /: ")
if operation == '+':
print(number1 + number2)
elif operation == '-':
print(number1 - number2)
elif operation == '*':
print(number1 * number2)
elif operation == '/':
print(number1 / number2 if number2 !=
0 else "Error. You cant divide by zero. Please select another operation")
else:
print("Error. There is no such operation. Please select another operation")
next_operation = input("Press 'y' to continue: ")
input()