-
Notifications
You must be signed in to change notification settings - Fork 17
/
loops.py
189 lines (125 loc) · 4.11 KB
/
loops.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
1.
Question 1
Fill in the blanks of this code to print out the numbers 1 through 7.
1 / 1 point
number = 1
while number in range(1,8):
print(number, end=" ")
number +=1
2.
Question 2
The show_letters function should print out each letter of a word on a separate line. Fill in the blanks to make that happen.
1 / 1 point
def show_letters(word):
for letter in word:
print(letter)
show_letters("Hello")
# Should print one line per letter
3.
Question 3
Complete the function digits(n) that returns how many digits the number has. For example: 25 has 2 digits and 144 has 3 digits. Tip: you can figure out the digits of a number by dividing it by 10 once per digit until there are no digits left.
1 / 1 point
def digits(n):
count = 0
if n == 0:
return 1
while (n > 0):
count += 1
n = n//10
return count
print(digits(25)) # Should print 2
print(digits(144)) # Should print 3
print(digits(1000)) # Should print 4
print(digits(0)) # Should print 1
4.
Question 4
This function prints out a multiplication table (where each number is the result of multiplying the first number of its row by the number at the top of its column). Fill in the blanks so that calling multiplication_table(1, 3) will print out:
1 2 3
2 4 6
3 6 9
1 / 1 point
def multiplication_table(start, stop):
for x in range(start, stop+1):
for y in range(start, stop+1):
print(str(x*y), end=" ")
print()
multiplication_table(1, 3)
# Should print the multiplication table shown above
5.
Question 5
The counter function counts down from start to stop when start is bigger than stop, and counts up from start to stop otherwise. Fill in the blanks to make this work correctly.
1 / 1 point
def counter(start, stop):
x = start
if start > stop:
return_string = "Counting down: "
while x >= stop:
return_string += str(x)
x-=1
if x >= stop:
return_string += ","
else:
return_string = "Counting up: "
while x <= stop:
return_string += str(x)
x+=1
if x <= stop:
return_string += ","
return return_string
print(counter(1, 10)) # Should be "Counting up: 1,2,3,4,5,6,7,8,9,10"
print(counter(2, 1)) # Should be "Counting down: 2,1"
print(counter(5, 5)) # Should be "Counting up: 5"
6.
Question 6
The even_numbers function returns a space-separated string of all positive numbers that are divisible by 2, up to and including the maximum that's passed into the function. For example, even_numbers(6) returns “2 4 6”. Fill in the blank to make this work.
1 / 1 point
def even_numbers(maximum):
return_string = ""
for x in range(2, maximum+1, 2):
return_string += str(x) + " "
return return_string.strip()
print(even_numbers(6)) # Should be 2 4 6
print(even_numbers(10)) # Should be 2 4 6 8 10
print(even_numbers(1)) # No numbers displayed
print(even_numbers(3)) # Should be 2
print(even_numbers(0)) # No numbers displayed
.
Question 7
The following code raises an error when executed. What's the reason for the error?
def decade_counter():
while year < 50:
year += 10
return year
1 / 1 point
Failure to initialize variables
Correct
Well done! The variable year needs to be initialized prior to being used in the while loop.
8.
Question 8
What is the value of x at the end of the following code?
for x in range(1, 10, 3):
print(x)
1 / 1 point
7
Correct
You got it! The upper limit of a range isn’t included, which means that the loop stops before reaching it. The increment is 3, so the loop stops when x reaches 7.
9.
Question 9
What is the value of y at the end of the following code?
for x in range(10):
for y in range(x):
print(y)
1 / 1 point
8
Correct
Great job! The upper limit of a range isn’t included, which means that the outer loop goes up to 9, so the highest upper limit for the inner loop is 9, which is also not included.
10.
Question 10
How does this function need to be called to print yes, no, and maybe as possible options to vote for?
def votes(params):
for vote in params:
print("Possible option:" + vote)
1 / 1 point
votes(['yes', 'no', 'maybe'])
Correct
Excellent! This function is looking for one argument, and the list of strings is just one argument.