-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrumentation_python_intro.py
More file actions
269 lines (200 loc) · 6.17 KB
/
instrumentation_python_intro.py
File metadata and controls
269 lines (200 loc) · 6.17 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# coding: utf-8
# ** Mac Robotics Club Tutorial 5: An Introduction to Coding in Python **
# Tuesday November 7, 2017
# Created by Amanda Boatswain Jacques
# In[3]:
## Printing to the screen
print('Hello')
print('World!')
# In[4]:
## Simple numerical operations
# (*) Means multiply, (/) means divide, (+) means to add, (-) means substract, etc.
print(2+2)
print(3*7)
print(30-2)
# In[5]:
## Creating numerical variables
a = 9/5
b = 20*a + 5 # use variable a in an operation
print('b = ')
print(b)
c = 30.985 # c is a floating point variable
print('datatype of c: ')
print (type(c))
# We can also assign multiple variables at once
d, e, f = 1, 2, 3
print('e = ')
print(e)
# ** Other types of variables: **
# In[6]:
## Creating strings
item_name = "Raspberry Pi"
print('item name: ')
print(item_name)
print(len(item_name)) # Returns the number of characters in the string
print(item_name[0]) # Find character in a particular place of the string
first_word = item_name[0:10]
print(first_word)
# Adding strings together
a = 'A'
b = 'B'
print(a+b+a) # Note that there are no spaces when characters are added together
# In[7]:
## Creating and Manipulating Lists
numbers = [67, 34, 51, 123, 10]
print('Numbers in list: ')
print(len(numbers))
print(numbers)
print(' ')
print('First number: ')
print(numbers[0])
print('First 3 numbers: ')
print(numbers[0:3])
print(' ')
# Change a value in the list
numbers[2] = 1
print('Third value changed: ')
print(numbers)
print(' ')
# Sort the numbers
numbers.sort()
print('Sorted numbers: ')
print(numbers)
print(' ')
# Remove a value from the list
numbers.pop() # default is the last number in the list
print('First number removed: ')
print(numbers)
print(' ')
print('Third number removed: ')
numbers.pop(2)
print(numbers)
print(' ')
# Insert a new value
numbers.insert(1, 66) # Arguments are position of insertion and new value
print(numbers)
print(' ')
# In[8]:
## Creating Dictionnaries
ages = {'Penny': 7, 'Alex': 10, 'Cynthia': 3}
print("Penny's age: ")
print(ages['Penny'])
ages['Penny'] = 8 # Let's say it was Penny's birthday yesterday
print('Updated ages: ')
print(ages) # Does not print in order!
# ** Loops, Logicals and Functions **
# In[9]:
## For Loops
import random
for x in range(0, 11):
print('x = ')
print(x)
for a in range(0,6):
random_number = random.randint(1,6)
print('random number = ')
print(random_number)
# In[10]:
## Using Logical Statements
# Boolean variables
print('Is 10 greater than 9? ')
print(10 > 9) # Returns a "True" value
k = 7
if k > 9: # Will check if this condition is true
print('k is large')
elif k > 7: # Else if (adds multiple cases)
print('k is fairly large')
else:
print('k is small')
# Try changing the value of k and see what prints next!
# In[11]:
## Dice Throwing using "if" statements
import random # import the random function
import time
for x in range (1, 11):
throw1 = random.randint(1,6) # choose a random integer between 1 and 6
throw2 = random.randint(1,6)
total = throw1 + throw2
time.sleep(2) # Will wait two seconds
print(total)
if total == 7:
print('Seven thrown!')
if total == 11:
print('Eleven thrown!')
if throw1 == throw2:
print('Double thrown!')
# In[12]:
## Dice Throwing using a "while" Loop
import random # import the random function
while True:
throw1 = random.randint(1,6) # choose a random integer between 1 and 6
throw2 = random.randint(1,6)
total = throw1 + throw2
print(total)
if (throw1 == 6 and throw2 == 6):
break # Will break out of the loop when a double six is thrown
print('Double Six Thrown!')
# **Let's create a Hangman Game using functions**
# *Taken from Programming the Raspberry Pi by Simon Monk*
# In[15]:
## Functions
import random
words = ['dog', 'cat', 'mouse', 'frog', 'chicken', 'snake'] # Create a list of words
lives_remaining = 14
guessed_letters = ' '
def play(): # Define the play game function
word = pick_a_word()
while True: # While loop will continue until the word is guessed or there are not more lives left
guess = get_guess(word)
if process_guess(guess, word):
print('You win!')
break
if lives_remaining == 0:
print('You are hung! :( ')
print('The word was: ' + word)
break
# Now let's write up the individual functions that will create our game.
def pick_a_word(): # Picks a word from the list at random
return random.choice(words)
def get_guess(word):
print_word_with_blanks(word)
print('Lives remaining: ' + str(lives_remaining))
guess = input('Guess a letter or whole word? ') # Prompts the user for a letter
return guess
def print_word_with_blanks(word):
display_word = ' '
for letter in word:
if guessed_letters.find(letter) > -1:
# Letter found
display_word = display_word + letter # Everytime a letter is properly guessed, it gets added to this string
else:
# Letter not found
display_word = display_word + '-'
print(display_word)
def process_guess(guess, word):
if len(guess) > 1: # If there is more than 1 letter, we assume it's a whole word guess
return whole_word_guess(guess, word)
else:
return single_letter_guess(guess, word)
def whole_word_guess(guess, word):
global lives_remaining # Global variable: Can be accessed from anywhere in the program
if guess.lower() == word.lower(): # Make sure the word/guesses are lowercase
return True
else:
lives_remaining = lives_remaining -1
return False
def single_letter_guess(guess, word):
global guessed_letters
global lives_remaining
if (word.find(guess) == -1):
# the letter was incorrect
lives_remaining = lives_remaining - 1
guessed_letters = guessed_letters + guess
if all_letters_guessed(word):
return True
return False
def all_letters_guessed(word):
for letter in word:
if (guessed_letters.find(letter) == -1):
return False # Searches through all the letters to see if they were all found
return True
play()