-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuiz10.py
More file actions
22 lines (21 loc) · 1.22 KB
/
Quiz10.py
File metadata and controls
22 lines (21 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# below is the AI 1.0 code, which works but cannot handle invalid input
# if the user input something other than an integer at first, the program will break due to a ValueError,
# caused by calling int() function on an non-integer input result
#
# Your task is to use the try-except-else-finally workflow to improve the existing code
# which can detect an invalid input in the beginning, and prints our an error message: 'Please input integers only.'
# then proceed to ask the user 'Do you want to play again? (y/N):' like the original function does
def interact():
while True: # keep looping until user reach break statement
try:
user_input = int(input('Please input an integer:'))
except ValueError:
print("Please input integers only")
else: # turn the user input into an integer
print('{} is {}.'.format(user_input, 'even' if user_input % 2 == 0 else 'odd')) # print out the message '{user_input} is {even/odd}.'
finally:
user_input = input('Do you want to play again? (y/N):')
if user_input != 'y': # quit if the user didn't input `y`
print('Goodbye.')
break # break the while loop to quit
interact()