-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_or_subt.py
62 lines (54 loc) · 2.03 KB
/
add_or_subt.py
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
# THIS IS A SIMPLE ADDITION AND SUBTRACTION PROGRAM
import random
# Input a name
print('\nHello! This is AddOrSubt app.\n')
print('Please enter your name:')
name = input()
print('\nHi %s!' % name.capitalize())
# Select A. Addition or B. Subtraction
while True:
print('Choose one (Click A or B): \nA. Addition\nB. Subtraction')
program_choice = input().lower()
def addition(num1, num2):
sum = num1 + num2
print('\nAnswer: ' + str(num1) + ' plus ' + str(num2) + ' is ' + str(sum) + '!')
def subtraction(num1, num2):
difference = num1 - num2
print('\nAnswer: ' + str(num1) + ' minus ' + str(num2) + ' is ' + str(difference) + '!')
if program_choice == 'a':
while True:
print('\nEnter a number:')
try:
num1 = int(input())
print('\n%s plus ...?' % str(num1))
num2 = int(input())
addition(num1, num2)
break
except ValueError:
print('Please enter an integer!')
elif program_choice == 'b':
while True:
print('\nEnter a number:')
try:
num1 = int(input())
print('\n%s minus ...?' % str(num1))
num2 = int(input())
subtraction(num1, num2)
break
except ValueError:
print('Please enter an integer!')
else:
print('\nClick A or B only.\n')
continue
# Print a random supportive word when player replay and thank you when player exits
supportWords = ['Great!', 'Nice!', 'Okay let\'s!', 'Let\'s go!', 'Awesome!', 'Okay, be fast!']
print('\nClick Enter to replay (or E to exit):')
replay_choice = input().lower()
if replay_choice == '':
print(supportWords[random.randint(0, len(supportWords))])
continue
elif replay_choice == 'e':
print('Thank you. Program exits.')
break
else:
print('Click enter or C')