@@ -32,24 +32,22 @@ print("It's not raining anymore.")
32
32
If you're not familiar with while loops, the program's output may be a
33
33
bit surprising:
34
34
35
- ```
36
- Oh crap, it's raining!
37
- Oh crap, it's raining!
38
- Oh crap, it's raining!
39
- Oh crap, it's raining!
40
- Oh crap, it's raining!
41
- Oh crap, it's raining!
42
- Oh crap, it's raining!
43
- Oh crap, it's raining!
44
- Oh crap, it's raining!
45
- Oh crap, it's raining!
46
- Oh crap, it's raining!
47
- Oh crap, it's raining!
48
- Oh crap, it's raining!
49
- Oh crap, it's raining!
50
- Oh crap, it's raining!
51
- (much more raining)
52
- ```
35
+ Oh crap, it's raining!
36
+ Oh crap, it's raining!
37
+ Oh crap, it's raining!
38
+ Oh crap, it's raining!
39
+ Oh crap, it's raining!
40
+ Oh crap, it's raining!
41
+ Oh crap, it's raining!
42
+ Oh crap, it's raining!
43
+ Oh crap, it's raining!
44
+ Oh crap, it's raining!
45
+ Oh crap, it's raining!
46
+ Oh crap, it's raining!
47
+ Oh crap, it's raining!
48
+ Oh crap, it's raining!
49
+ Oh crap, it's raining!
50
+ (much more raining)
53
51
54
52
Again, this program does not break your computer. It just prints the
55
53
same thing multiple times. We can interrupt it by pressing Ctrl+C.
@@ -76,17 +74,15 @@ print("It's not raining anymore.")
76
74
77
75
Running the program may look like this:
78
76
79
- ```
80
- It's raining!
81
- Or is it? (y=yes, n=no) i dunno
82
- Enter y or n next time.
83
- It's raining!
84
- Or is it? (y=yes, n=no) y
85
- Oh well...
86
- It's raining!
87
- Or is it? (y=yes, n=no) n
88
- It's not raining anymore.
89
- ```
77
+ It's raining!
78
+ Or is it? (y=yes, n=no) i dunno
79
+ Enter y or n next time.
80
+ It's raining!
81
+ Or is it? (y=yes, n=no) y
82
+ Oh well...
83
+ It's raining!
84
+ Or is it? (y=yes, n=no) n
85
+ It's not raining anymore.
90
86
91
87
We can also interrupt a loop even if the condition is still true using
92
88
the ` break ` keyword. In this case, we'll set condition to True and rely
@@ -106,14 +102,12 @@ while True:
106
102
107
103
The program works like this:
108
104
109
- ``` py
110
- Is it raining? (y=yes, n=no) who knows
111
- Enter y or n.
112
- Is it raining? (y=yes, n=no) y
113
- It' s raining!
114
- Is it raining? (y=yes, n=no) n
115
- It' s not raining anymore.
116
- ```
105
+ Is it raining? (y=yes, n=no) who knows
106
+ Enter y or n.
107
+ Is it raining? (y=yes, n=no) y
108
+ It's raining!
109
+ Is it raining? (y=yes, n=no) n
110
+ It's not raining anymore.
117
111
118
112
## Until loops
119
113
@@ -146,13 +140,11 @@ print(stuff[4])
146
140
147
141
The output of the program is like this:
148
142
149
- ``` py
150
- hello
151
- hi
152
- how are you doing
153
- im fine
154
- how about you
155
- ```
143
+ hello
144
+ hi
145
+ how are you doing
146
+ im fine
147
+ how about you
156
148
157
149
But this is only going to print five items, so if we add something to
158
150
stuff, it's not going to be printed. Or if we remove something from
@@ -161,7 +153,8 @@ stuff, we'll get an error saying "list index out of range".
161
153
We could also create an index variable, and use a while loop:
162
154
163
155
``` py
164
- >> > length_of_stuff = len (stuff) # len(x) is the length of x
156
+ >> > stuff = [' hello' , ' hi' , ' how are you doing' , ' im fine' , ' how about you' ]
157
+ >> > length_of_stuff = len (stuff) # len(x) is the length of x, 5 in this case
165
158
>> > index = 0
166
159
>> > while index < length_of_stuff:
167
160
... print (stuff[index])
@@ -181,7 +174,6 @@ a lot of stuff to worry about for just printing each item.
181
174
This is when for loops come in:
182
175
183
176
``` py
184
- >> > stuff = [' hello' , ' hi' , ' how are you doing' , ' im fine' , ' how about you' ]
185
177
>> > for thing in stuff:
186
178
... # this is repeated for each element of stuff, that is, first
187
179
... # for stuff[0], then for stuff[1], etc.
@@ -236,7 +228,7 @@ Instead, you can create a copy of stuff and loop over it.
236
228
>> >
237
229
```
238
230
239
- Or if you want to clear a list, just use the ` .clear() ` list method.
231
+ Or if you want to clear a list, just use the ` .clear() ` list method:
240
232
241
233
``` py
242
234
>> > stuff = [' hello' , ' hi' , ' how are you doing' , ' im fine' , ' how about you' ]
@@ -246,7 +238,7 @@ Or if you want to clear a list, just use the `.clear()` list method.
246
238
>> >
247
239
```
248
240
249
- ## Excercises
241
+ ## Exercises
250
242
251
243
1 . Back in "Using if, else and elif" we created a program that asked
252
244
for username and password and checks them, and we made users "foo"
@@ -256,8 +248,8 @@ Or if you want to clear a list, just use the `.clear()` list method.
256
248
257
249
``` py
258
250
users = [
259
- [' foo' , ' bar ' ],
260
- [' biz ' , ' baz' ],
251
+ [' foo' , ' biz ' ],
252
+ [' bar ' , ' baz' ],
261
253
]
262
254
```
263
255
0 commit comments