Skip to content

Commit 2d3c0c1

Browse files
committed
work on if.md
1 parent f5c9b09 commit 2d3c0c1

File tree

1 file changed

+111
-33
lines changed

1 file changed

+111
-33
lines changed

if.md

Lines changed: 111 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 4. Using if, elif, else and while
22

3-
### Using if statements
3+
## Using if statements
44

55
Now we know what True and False are.
66

@@ -34,28 +34,72 @@ It's raining!
3434

3535
The prompt changed from `>>>` to `...`. It meant that Python was
3636
expecting me to keep typing. When I was done, I just pressed Enter
37-
twice. My code was execute and the prompt was restored to `>>>`.
37+
twice. My code was execute and the prompt was restored to `>>>`. IDLE
38+
does this a bit differently, so if you use IDLE, running this example
39+
code looks more like this:
40+
41+
```py
42+
>>> its_raining = True
43+
>>> if its_raining:
44+
print("It's raining!")
45+
46+
47+
It's raining!
48+
>>> ```
3849

3950
At this point it's easier to put your code into a file and use it
4051
there. If you use IDLE, go to File at top left and select New File, or
4152
just press Ctrl+N.
4253

4354
![New File in IDLE](idle-new.png)
4455

45-
If you are not using IDLE, open whatever editor you have installed, or
46-
install
56+
If you don't use IDLE, please take the time to
57+
[set up your editor correctly](editor-setup.md).
4758

48-
The file's content would be like this:
59+
Create a file called `rain.py`, and type the following content into it:
4960

5061
```py
5162
its_raining = True
5263
if its_raining:
5364
print("It's raining!")
5465
```
5566

56-
### Using else
67+
The line with a print is **indented** by four spaces. Indentation is
68+
important in Python. If the indentation is not consistent, we may get
69+
an error or our program may do something else than we wanted it to.
5770

58-
What if you want to print a different message if it's not raining? You could do something like this:
71+
Now run the rain program. Most editors (including IDLE) will run your
72+
code when you press F5. If your editor doesn't, run it from PowerShell,
73+
command prompt or terminal. You probably need to first go to wherever
74+
you saved your file. with `cd`. For example, if the file is on your
75+
desktop, type `cd Desktop` before running the file.
76+
77+
Running from IDLE looks like this:
78+
79+
>>>
80+
========================= RESTART: /home/you/rain.py =========================
81+
It's raining!
82+
>>>
83+
84+
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:
59103

60104
```py
61105
its_raining = True # you can change this to False
@@ -67,7 +111,8 @@ if its_not_raining:
67111
print("It's not raining.")
68112
```
69113

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.
71116

72117
You can also add `not its_raining` directly to the second if statement:
73118

@@ -91,7 +136,8 @@ else:
91136
print("It's not raining.")
92137
```
93138

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.
95141

96142
```py
97143
print("Hello!")
@@ -117,11 +163,14 @@ The program prints different things depending on what you enter.
117163
Access denied.
118164
>>>
119165

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.
121169

122-
### Avoiding many levels of indentation with elif
170+
## Avoiding many levels of indentation with elif
123171

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.
125174

126175
```py
127176
print("Hello!")
@@ -145,7 +194,8 @@ else:
145194
print("I don't know what", word, "means.")
146195
```
147196

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:
149199

150200
```py
151201
print("Hello!")
@@ -167,19 +217,19 @@ else:
167217

168218
### Repeating with while loops
169219

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`**.
171223

172224
```py
173225
its_raining = True
174226
while its_raining:
175227
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
177229
```
178230

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:
180232

181-
>>> ================================ RESTART ================================
182-
>>>
183233
It's raining!
184234
It's raining!
185235
It's raining!
@@ -189,31 +239,59 @@ Don't get scared when you run the program. Like I wrote in the introduction, thi
189239
It's raining!
190240
(many more lines of raining)
191241

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.
193250

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.
195255

196256
```py
197257
while True:
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':
200260
print("It's raining!")
201-
elif answer == "n":
261+
elif answer == 'n':
202262
print("It's not raining.")
263+
elif answer == 'q':
264+
break
203265
else:
204-
print("Please enter 'y' or 'n'.")
266+
print("Please enter 'y', 'n' or 'q'.")
205267
```
206268

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
288+
again if he mistypes the password.
215289
4. Can you limit the number of attempts to 3?
216290

217291
The answers are [here](answers.md).
218292

219-
[Previous](3.md) | [Next](5.md) | [Home](README.md)
293+
***
294+
295+
You may use this tutorial freely at your own risk. See [LICENSE](LICENSE).
296+
297+
[Back to the list of contents](README.md)

0 commit comments

Comments
 (0)