-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic calculator
More file actions
67 lines (52 loc) · 1.73 KB
/
Basic calculator
File metadata and controls
67 lines (52 loc) · 1.73 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
63
64
65
66
67
#Donald's Basic calculator
#this function adds two numbers
def add(x,y):
return x + y
#this function subtracts two numbers
def subtract(x,y):
return x-y
#this function multiplies two numbers
def multiply(x,y):
return x * y
#this function divides two numbers
def divide(x,y):
return x / y
#this function provides the modulus of two numbers
def modulus(x,y):
return x % y
print('''Welcome!
Simple calculator
Select operation.''')
print('1.add')
print('2.subtract')
print('3.multiply')
print('4.divide')
print('5.modulus')
while True:
#take input from the user
choice = input('Enter choice(1/2/3/4/5):')
#check if the choice is one of the five options
if choice in ('1','2','3','4','5'):
try:
num1 = float(input('Enter first number:'))
num2 = float(input('Enter second number'))
except ValueError:
print("Invalid input. Please enter a number.")
continue
if choice == '1':
print(num1, '+', num2, '=', add(num1, num2))
elif choice == '2':
print (num1, '+', num2, '=', subtract(num1, num2))
elif choice == '3':
print (num1, '+', num2, '=', multiply(num1, num2))
elif choice == '4':
print (num1, '+', num2, '=', divide(num1, num2))
elif choice == '5':
print (num1, '+', num2, '=', modulus(num1, num2))
#check if user wants another calculation
# break the while loop if the answer is no
next_calculation = input ("Let's do a different calculation?(yes/no):")
if next_calculation == 'no':
break
else:
print('invalid input')