You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
And running from the Windows PowerShell or command prompt looks like
85
+
this:
86
+
87
+
C:\Users\You> cd Desktop
88
+
C:\Users\You\Desktop> py rain.py
89
+
It's raining!
90
+
C:\Users\You\Desktop>
91
+
92
+
Running from a terminal looks like this:
93
+
94
+
you@YourComputer:~$ cd Desktop
95
+
you@YourComputer:~/Desktop$ python3 rain.py
96
+
It's raining!
97
+
you@YourComputer:~/Desktop$
98
+
99
+
## Using else
100
+
101
+
What if you want to print a different message if it's not raining? You
102
+
could do something like this:
59
103
60
104
```py
61
105
its_raining =True# you can change this to False
@@ -67,7 +111,8 @@ if its_not_raining:
67
111
print("It's not raining.")
68
112
```
69
113
70
-
Now your program will print a different value depending on what the value of `its_raining` is.
114
+
Now your program will print a different value depending on what the
115
+
value of `its_raining` is.
71
116
72
117
You can also add `not its_raining` directly to the second if statement:
73
118
@@ -91,7 +136,8 @@ else:
91
136
print("It's not raining.")
92
137
```
93
138
94
-
By combining that with the input function we can make a program that asks for a password and checks if it's correct.
139
+
By combining that with the input function we can make a program that
140
+
asks for a password and checks if it's correct.
95
141
96
142
```py
97
143
print("Hello!")
@@ -117,11 +163,14 @@ The program prints different things depending on what you enter.
117
163
Access denied.
118
164
>>>
119
165
120
-
Using the input function for passwords doesn't work very well because we can't hide the password with asterisks. There are better ways to get a password from the user, but you shouldn't worry about that just yet.
166
+
Using the input function for passwords doesn't work very well because
167
+
we can't hide the password with asterisks. There are better ways to get
168
+
a password from the user, but you shouldn't worry about that just yet.
121
169
122
-
###Avoiding many levels of indentation with elif
170
+
## Avoiding many levels of indentation with elif
123
171
124
-
If you have more than one condition to check, your code will end up looking a bit messy.
172
+
If you have more than one condition to check, your code will end up
173
+
looking a bit messy.
125
174
126
175
```py
127
176
print("Hello!")
@@ -145,7 +194,8 @@ else:
145
194
print("I don't know what", word, "means.")
146
195
```
147
196
148
-
Instead of typing `else`, indenting more and typing an `if` you can simply type `elif`, which is short for `else if`. Like this:
197
+
Instead of typing `else`, indenting more and typing an `if` you can
198
+
simply type `elif`, which is short for `else if`. Like this:
149
199
150
200
```py
151
201
print("Hello!")
@@ -167,19 +217,19 @@ else:
167
217
168
218
### Repeating with while loops
169
219
170
-
While loops are similar to if statements. The only difference is that at the end of the indented block they go back to the line with the word `while` and the condition.
220
+
While loops are similar to if statements. The only difference is that
221
+
at the end of the indented block they jump **back to the line with the
222
+
word `while`**.
171
223
172
224
```py
173
225
its_raining =True
174
226
while its_raining:
175
227
print("It's raining!")
176
-
#We'll jump back to the second line from here
228
+
#we'll jump back to the second line from here
177
229
```
178
230
179
-
Don't get scared when you run the program. Like I wrote in the introduction, this will not destroy or crash your computer. It just repeats the same thing quickly. You can interrupt the program by hitting Ctrl+C. You'll get an error message saying that a `KeyboardInterrupt` occurred, that's normal. The output is like this:
231
+
When you run the program, it keeps printing `It's raining!` forever:
@@ -189,31 +239,59 @@ Don't get scared when you run the program. Like I wrote in the introduction, thi
189
239
It's raining!
190
240
(many more lines of raining)
191
241
192
-
What happened is that the indented while block ran, just like an if block. Then, at the end of the block we moved back to the beginning of the while loop. `its_raining` was still `True`, so the indented block ran again, and so on.
242
+
This program will not destroy or crash your computer. It just repeats
243
+
the same thing quickly, but not quickly enough to damage anything. You
244
+
can interrupt the program by hitting Ctrl+C.
245
+
246
+
The indented while block ran, just like an if block. Then, at the end
247
+
of the block we moved back to the beginning of the while loop.
248
+
`its_raining` was still `True`, so the indented block ran again, and so
249
+
on.
193
250
194
-
While loops are often used for repeating things forever _[*]_ with a `while True`.
251
+
While loops are often used for repeating things and endless number of
252
+
times with a `while True`. These are called **infinite loops**. You can
253
+
actually stop an infinite loop, just add a `break` into it. If you have
254
+
two loops inside each other, `break` will break the outermost loop.
195
255
196
256
```py
197
257
whileTrue:
198
-
answer =input("Is it still raining? (y/n) ")
199
-
if answer =="y":
258
+
answer =input("Is it still raining? (y=yes/n=no/q=quit) ")
259
+
if answer =='y':
200
260
print("It's raining!")
201
-
elif answer =="n":
261
+
elif answer =='n':
202
262
print("It's not raining.")
263
+
elif answer =='q':
264
+
break
203
265
else:
204
-
print("Please enter 'y'or 'n'.")
266
+
print("Please enter 'y', 'n' or 'q'.")
205
267
```
206
268
207
-
Again, you can interrupt the program with Ctrl+C.
208
-
209
-
_[*] There are many ways to interrupt while loops, including `while True` loops. `break` will end the innermost loop it's in, and `exit()` will quit the whole program._
210
-
211
-
### Exercises
212
-
1. Make a program that asks for a password and prints `Welcome!`, `Access denied` or `You didn't enter anything` depending on whether the user entered the correct password, a wrong password, or nothing at all by pressing Enter without typing anything.
213
-
2. Make a program that asks for username and password and checks them. Make users "foo" and "bar" with passwords "biz" and "baz".
214
-
3. Make a program that asks for username and password and gives the user an infinite number of attempts, so the user can always try again if he mistypes the password.
269
+
## Summary
270
+
271
+
- Indentation is important in Python.
272
+
- Indented code under an if statement runs if the condition is true.
273
+
- While works just like if, but it jumps back to the line with the word
274
+
`while` when it gets to the end of the indented block.
275
+
-`while True:` runs the loop until you interrupt it, for example with
276
+
Ctrl+C or a `break`.
277
+
278
+
## Exercises
279
+
280
+
1. Make a program that asks for a password and prints `Welcome!`,
281
+
`Access denied` or `You didn't enter anything` depending on whether
282
+
the user entered the correct password, a wrong password, or nothing
283
+
at all by pressing Enter without typing anything.
284
+
2. Make a program that asks for username and password and checks them.
285
+
Make users "foo" and "bar" with passwords "biz" and "baz".
286
+
3. Make a program that asks for a username and a password and gives the
287
+
user an infinite number of attempts, so the user can always try
0 commit comments