-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimple_user_interaction.py
93 lines (54 loc) · 2.39 KB
/
Simple_user_interaction.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
game_list = [0,1,2]
def display_game(game_list):
print("Here is the current list")
print(game_list)
def position_choice():
# This original choice value can be anything that isn't an integer
choice = 'wrong'
# While the choice is not a digit, keep asking for input.
while choice not in ['0','1','2']:
# we shouldn't convert here, otherwise we get an error on a wrong input
choice = input("Pick a position to replace (0,1,2): ")
if choice not in ['0','1','2']:
# THIS CLEARS THE CURRENT OUTPUT BELOW THE CELL
print("Sorry, but you did not choose a valid position (0,1,2)")
# Optionally you can clear everything after running the function
# clear_output()
# We can convert once the while loop above has confirmed we have a digit.
return int(choice)
def replacement_choice(game_list,position):
user_placement = input("Type a string to place at the position")
game_list[position] = user_placement
return game_list
def gameon_choice():
# This original choice value can be anything that isn't a Y or N
choice = 'wrong'
# While the choice is not a digit, keep asking for input.
while choice not in ['Y','N']:
# we shouldn't convert here, otherwise we get an error on a wrong input
choice = input("Would you like to keep playing? Y or N ")
if choice not in ['Y','N']:
# THIS CLEARS THE CURRENT OUTPUT BELOW THE CELL
print("Sorry, I didn't understand. Please make sure to choose Y or N.")
# Optionally you can clear everything after running the function
# clear_output()
if choice == "Y":
# Game is still on
return True
else:
# Game is over
return False
# Variable to keep game playing
game_on = True
# First Game List
game_list = [0,1,2]
while game_on:
# Clear any historical output and show the game list
display_game(game_list)
# Have player choose position
position = position_choice()
# Rewrite that position and update game_list
game_list = replacement_choice(game_list,position)
# Clear Screen and show the updated game list
# Ask if you want to keep playing
game_on = gameon_choice()