-
Notifications
You must be signed in to change notification settings - Fork 1
/
October10.py
193 lines (127 loc) · 3.65 KB
/
October10.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
# This is the grading program we looked at earlier
# 4) Grading program - if score is > 90 you get an A,
## > 80 < 90 you get a B
## > 70 < 80 a C
## > 60 < 70 a D
## anything else you flunk
grade = 55
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
elif grade >= 60:
print("D")
else:
print("You Flunked!")
# We changed it to a Function:
def grader(grade):
if grade >= 90:
return "A"
elif grade >= 80:
return "B"
elif grade >= 70:
return "C"
elif grade >= 60:
return "D"
else:
return "You Flunked!"
g = grader(60)
print(g)
# to get grades for the class use a list
grades = [75,80,90,60,65,85]
# find the class average grade and use the function, grader, we just wrote
def term_grader(grade):
term = sum(grade) / len(grade)
return grader(term)
print(term_grader(grades))
## Note, I cheated with the sum function:
#
# Here is a home grown one
#
def sum_list(items):
sum_numbers = 0
for x in items:
sum_numbers += x
return sum_numbers
def term_grader2(grade):
term = sum_list(grade) / len(grade)
return grader(term)
print(term_grader2(grades))
# 1. Write a program to multiply the numbers in a list.
## Hint *= is a thing
def multiply_list(items):
mult_num = 1
for x in items:
mult_num *= x
return mult_num
nums = [ 3,5,2,4]
multiply_list(nums)
# 2. Write a program to get the largest number in a list
## Coding takes practice more than brains
## https://exercism.io/ is a good practice site for exercises like these
# A simple way
def largest(items):
items.sort()
return items[-1]
largest(nums)
## A do it yourself way
def my_largest(items):
my_max = items[0]
for x in items:
if my_max < x:
my_max = x
return my_max
my_largest(nums)
# 3. Assume:
law_class = ["Matthew", "Hunter", "Marianna", "Brittany", "Harrison", "James","Bradley"]
# Print "Hunter"
print(law_class[1])
# remember a list is similar to an array in other languages it starts with 0
# 4. Print out each name in law_class
## Python provides an easy way to move through a list " for x in list: "
for x in law_class:
print(x,end=" ")
# 5. Put the law_class in alphabetical order and print it
law_class.sort()
for x in law_class:
print(x,end=" ")
## list - order matters
fam = ["Tom", "Carole", "Baby Brooke"]
fam2 = ["Carole", "Tom", "Baby Brooke"]
fam == fam2 # False
## dictionary Order doesn't matter
fam = {'husband': 'Tom', 'wife': 'Carole', 'grand_baby': 'Baby Brooke'}
fam2 = {'wife': 'Carole', 'grand_baby': 'Baby Brooke', 'husband': 'Tom'}
## Dictionary like json Javascript Object Notation - similar notation
## json key must be string with double quotes
# { "family":
# {"husband": "Tom",
# "wife": "Carole",
# "grand baby": "baby brooke"}
# }
fam == fam2 # True
1. ## print the keys in fam
2. ## print the values in fam
3. work through the tic tac toe board page 113
# Tic Tac Toe Board
theBoard = {'top-L': ' ', 'top-M':' ', 'top-R':' ',
'mid-L': ' ', 'mid-M':' ', 'mid-R':' ',
'low-L': ' ', 'low-M':' ', 'low-R':' '}
def printBoard(board):
print(' '+ board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
print('-+-+-')
print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['top-R'])
print('-+-+-')
print(board['low-L'] + '|' + board['low-M'] + '|' + board['low-R'])
turn = 'x'
for i in range(9):
print('Turn for ' + turn + '_ on which space? ')
move = input()
theBoard[move] = turn
if turn == 'x':
turn = '0'
else:
turn = 'x'
printBoard(theBoard)