forked from a-newman/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_while_loops.py
More file actions
61 lines (46 loc) · 2.1 KB
/
8_while_loops.py
File metadata and controls
61 lines (46 loc) · 2.1 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
import random
# ** While Loops **
# What if you want your code to do something over and over again until some
# condition is met?
# For instance, maybe you're writing code for a timer
# and you want to keep checking how much time has passed until you have waited
# the correct amount of time.
# Then you should use a while loop. Check out the example:
time_to_count = 10
seconds_passed = 0
while seconds_passed < time_to_count: # this is called the condition
print("ticks_passed:", seconds_passed)
seconds_passed += 1 # increase seconds_passed by 1
print("Timer done!")
print("\n")
# At the beginning of the loop, the condition (`seconds_passed < time_to_count`)
# is evaluated. If the condition is `True`, then the body of the loop--the
# indented block that follows the while condition--is run. If the condition
# is `False`, then it continues with the rest of the code.
# A really important thing to consider when writing a while loop is
# "termination": making sure that at some point, the condition will evaluate to
# `False`. Otherwise, the loop will run forever!
# ** Break **
# There is one exception to the idea of termination. Consider this while loop:
n_tries = 0
while True:
n_tries += 1
n = random.randint(1, 10) # chooses a random number between 1 and 10
if n == 10:
break
print("Outside of loop; took", n_tries, "tries")
# Clearly, the condition here will never be `False`! They key here is the word
# `break`. This keyword causes Python to "break" out of the loop and continue
# with the next line of code. Note that writing a "while True" loop can be
# dangerous, because it is not clear when the loop will terminate. If possible,
# state the condition explicitly. You should reserve "while True" for
# situations where you really do have to continue doing something forever, or
# where it is not clear how many times you will have to do something.
# ** Exercises **
# 1. Write a while loop that prints all the numbers from 1 to 10.
# Your code here.
# 2. What is wrong with this code?
# count = 10
# while (count < 100):
# print(count)
# count = count - 1