-
Notifications
You must be signed in to change notification settings - Fork 17
/
whileloops.py
214 lines (163 loc) · 6.59 KB
/
whileloops.py
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
# 1.
# Question 1
# What are while loops in Python?
#
# 1 / 1 point
#
# While loops let the computer execute a set of instructions while a condition is true.
# 2.
# Question 2
# Fill in the blanks to make the print_prime_factors function print all the prime factors of a number. A prime factor is a number that is prime and divides another without a remainder.
def print_prime_factors(number):
# Start with two, which is the first prime
factor = 2
# Keep going until the factor is larger than the number
while factor <= number:
# Check if factor is a divisor of number
if number % factor == 0:
# If it is, print it and divide the original number
print(factor)
number = number / factor
else:
# If it's not, increment the factor by one
factor += 1
return "Done"
print_prime_factors(100)
# Should print 2,2,5,5
# DO NOT DELETE THIS COMMENT
# 3.
# Question 3
# The following code can lead to an infinite loop. Fix the code so that it can finish successfully for all numbers.
# Note: Try running your function with the number 0 as the input, and see what you get!
def is_power_of_two(n):
# Check if the number can be divided by two without a remainder
while n % 2 == 0 and n != 0:
n = n / 2
# If after dividing by two the number is 1, it's a power of two
if n == 1:
return True
n += 1
return False
print(is_power_of_two(0)) # Should be False
print(is_power_of_two(1)) # Should be True
print(is_power_of_two(8)) # Should be True
print(is_power_of_two(9)) # Should be False
# 4.
# Question 4
# Fill in the empty function so that it returns the sum of all the divisors of a number, without including it. A divisor is a number that divides into another without a remainder.
def sum_divisors(n):
i = 1
sum = 0
# Return the sum of all divisors of n, not including n
while i < n:
if n % i == 0:
sum += i
i += 1
else:
i += 1
return sum
print(sum_divisors(0))
# 0
print(sum_divisors(3)) # Should sum of 1
# 1
print(sum_divisors(36)) # Should sum of 1+2+3+4+6+9+12+18
# 55
print(sum_divisors(102)) # Should be sum of 2+3+6+17+34+51
# 114
#
# 5.
# Question 5
# The multiplication_table function prints the results of a number passed to it multiplied by 1 through 5. An additional requirement is that the result is not to exceed 25, which is done with the break statement. Fill in the blanks to complete the function to satisfy these conditions.
def multiplication_table(number):
# Initialize the starting point of the multiplication table
multiplier = 1
# Only want to loop through 5
while multiplier <= 5:
result = multiplier * number
# What is the additional condition to exit out of the loop?
if result > 25:
break
print(str(number) + "x" + str(multiplier) + "=" + str(result))
# Increment the variable for the loop
multiplier += 1
multiplication_table(3)
# Should print: 3x1=3 3x2=6 3x3=9 3x4=12 3x5=15
multiplication_table(5)
# Should print: 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25
multiplication_table(8)
# Should print: 8x1=8 8x2=16 8x3=24
# A while loop will continuously execute code depending on the value of a condition. It begins with the keyword while, followed by a comparison to be evaluated, then a colon. On the next line is the code block to be executed, indented to the right. Similar to an if statement, the code in the body will only be executed if the comparison is evaluated to be true. What sets a while loop apart, however, is that this code block will keep executing as long as the evaluation statement is true. Once the statement is no longer true, the loop exits and the next line of code will be executed.
# Question
x = 0
while x < 5:
print("Not there yet, x=" + str(x))
x = x + 1
print("x=" + str(x))
# How many times will "Not there yet" be printed?
#
# 5
# Correct
# You got it! The variable x starts at 0 and gets incremented once per iteration, so there are 5 iterations for which x is smaller than 5.
#
#
#
# Question
# Can you work out what this function does? Try passing different parameters to the attempts function to see what it does.
def attempts(n):
x = 1
while x <= n:
print("Attempt " + str(x))
x += 1
print("Done")
attempts(5)
# Attempt 1
# Attempt 2
# Attempt 3
# Attempt 4
# Attempt 5
# Done
# #Initializing Variables
# In this code, there's an initialization problem that's causing our function to behave incorrectly. Can you find the problem and fix it?
def count_down(current):
while (current > 0):
print(current)
current -= 1
print("Zero!")
count_down(3)
# Here is your output:
# 3
# 2
# 1
# Zero!
#
# You nailed it! By initializing the current variable you got
# the function to behave correctly.
#
# You'll want to watch out for a common mistake: forgetting to initialize variables. If you try to use a variable without first initializing it, you'll run into a NameError. This is the Python interpreter catching the mistake and telling you that you’re using an undefined variable. The fix is pretty simple: initialize the variable by assigning the variable a value before you use it.
#
# Another common mistake to watch out for that can be a little trickier to spot is forgetting to initialize variables with the correct value. If you use a variable earlier in your code and then reuse it later in a loop without first setting the value to something you want, your code may wind up doing something you didn't expect. Don't forget to initialize your variables before using them!
#
# #Breaking infinite loops
# The following code causes an infinite loop. Can you figure out what’s missing and how to fix it?
def print_range(start, end):
# Loop through the numbers from start to end
n = start
while n <= end:
print(n)
n += 1
print_range(1, 5) # Should print 1 2 3 4 5 (each number on its own line)
# Here is your output:
# 1
# 2
# 3
# 4
# 5
#
# Great work! You've managed to fix the error in the code that
# was causing an infinite loop!
# #Infinite loops and Code Blocks
# Another easy mistake that can happen when using loops is introducing an infinite loop. An infinite loop means the code block in the loop will continue to execute and never stop. This can happen when the condition being evaluated in a while loop doesn't change. Pay close attention to your variables and what possible values they can take. Think about unexpected values, like zero.
#
# In the Coursera code blocks, you may see an error message that reads "Evaluation took more than 5 seconds to complete." This means that the code encountered an infinite loop, and it timed out after 5 seconds. You should take a closer look at the code and variables to spot where the infinite loop is.
#
#