-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfor_loops_day_3_4pm.py
199 lines (160 loc) · 4.38 KB
/
for_loops_day_3_4pm.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"""
nested for loops
2d lists
maybe while but probably wednesday
"""
size = int(input('What size do you want? '))
for i in range(size):
for j in range(size):
print(f'({i}, {j})', end=' ')
print()
for i in range(size):
for j in range(size):
if i >= j:
print('*', end='')
print()
print('\n')
for i in range(size):
for j in range(size):
if i + j <= size - 1:
print('*', end='')
print()
"""
Want:
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
"""
for i in range(size):
for j in range(size):
if i > j:
print(' ', end='')
else:
print('*', end='')
print()
for i in range(size):
for j in range(size):
if i + j < size - 1:
print(' ', end='')
else:
print('*', end='')
print()
if False:
for i in range(size):
print(f'i is {i}')
for j in range(size):
print(f'j is {j}')
input('>> ')
"""
Last week we "remember" that lists can be composed of data
data can be ints, floats, bools, strings
data can also be lists
"""
my_list = [
[1, 2, 3], # index 0
[4, 5, 6], # index 1
[9, 8, 7], # index 2
[10, 11, 13] # index 3
]
print("L = ", len(my_list))
my_index = int(input("What index do you want? "))
if 0 <= my_index < len(my_list):
print(my_list[my_index])
else:
print('Bad human!')
"""
The super important thing to know is that the inner list
can have a different length to the outer list
What if I want to access a particular value?
"""
print(my_list[1][1], my_list[2][0])
my_list[2][2] = 14
print(my_list)
for row in my_list:
print(row)
wierd_list = [
[3, 9, 12, 15],
[7, 77, 343, 28, 49],
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144],
['a', 'b', 'c']
]
wierd_list.append([4, 5, 6])
for row in range(len(wierd_list)):
for j in range(len(wierd_list[row])):
print(wierd_list[row][j], end='\t')
print()
number_list = [1, 2, 3, 4, 5, 10, 12, 15, 20]
position = int(input('Where to insert (the actual position)? '))
what_to_insert = int(input('Give me a number: '))
number_list.append(0)
for i in range(len(number_list) - 2, position - 1, -1):
number_list[i + 1] = number_list[i]
number_list[position] = what_to_insert
print(number_list)
number_list.insert(3, 15)
print(number_list)
"""
TicTacToe
Pick X or O
keep track of the turn (use turn % 2 to figure out which turn it is)
we need a board which is a 2d list (3x3)
get user input and validate (check to make sure that the user
has entered a board position, board must be empty there)
victory check <-- we're going to work on this part
"""
my_board = [
[' ', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' '],
]
for i in range(len(my_board)):
first_element = my_board[i][0]
if first_element == ' ':
row_victory = False
else:
row_victory = True
for j in range(len(my_board[i])):
if my_board[i][j] != first_element:
row_victory = False
if row_victory:
print(first_element, 'has won')
for col in range(len(my_board)):
first_element = my_board[0][col]
if first_element == ' ':
col_victory = False
else:
col_victory = True
for row in range(len(my_board[i])):
if my_board[row][col] != first_element:
col_victory = False
if col_victory:
print(first_element, 'has won')
if anti_diag_element != ' ':
diag_victory = True
else:
diag_victory = False
diag_victory = True
for i in range(len(my_board)):
if my_board[i][i] != diag_element:
diag_victory = False
if diag_victory:
print('Diagonal victory for ', diag_element)
anti_diag_element = my_board[0][0]
if anti_diag_element != ' ':
anti_diag_victory = True
else:
anti_diag_victory = False
for i in range(len(my_board)):
if my_board[i][len(my_board) - 1 - i] != anti_diag_element:
anti_diag_victory = False
if anti_diag_victory:
print('Anti-Diagonal victory for ', anti_diag_element)