1
2
3
4
5
6
7
8
9
10
11
12
To practice on your own, or to check code you believe shouldn't have been scored as incorrect, go to Trinket.
Post a review on Amazon for the paperback or the Kindle edition.
Email me to give me a compliment, complaint, or correction. I'll respond.
0,1,2,3,4,5,6,7,8,9,10,11
0
0
0
0
How many times will the if statement run? Answer with a numeral. | chosen_letter = "b" letter_list = ["a", "b", "c", "d", "e", "f"] for a_letter in letter_list: if a_letter == chosen_letter: print("It's a match.") break | 2 — When there's a match, the break statement interrupts the loop and there's more testing. | ^ *2 *$ |
The next line interrupts the loop when there's a match. Code it. Remember to indent 2 tabs. | chosen_letter = "b" letter_list = ["a", "b", "c", "d", "e", "f"] for a_letter in letter_list: if a_letter == chosen_letter: print("It's a match.") | break | ^ * break *$ |
Code the first line of a for loop. The list name is letter_list. The variable name is a_letter. | for a_letter in letter_list: | for `a_letter `in `letter_list`: | |
Code the first line of a loop that runs inside this loop. The list name is b and the variable name is a. Don't forget to indent. | for x in y: | for a in b: | for a in b: |
Code the first line of a for loop, and then the first line of an inner loop. Make up all the names. Don't forget to indent the second line. | for a in b: for x in y: | ^ *for [a-zA-Z_][a-zA-Z0-9_]* in [a-zA-Z_][a-zA-Z0-9_]*:\n for [a-zA-Z_][a-zA-Z0-9_]* in [a-zA-Z_][a-zA-Z0-9_]*: *$ | |
Code the first line of a for loop, then the first and second lines of an inner loop. The second line of the inner loop displays the element in the inner list. Make up the names. Remember to indent line 2 1 tab and line 3 2 tabs. | for a in b: for x in y: print(x) | ^ *for [a-zA-Z_][a-zA-Z0-9_]* in [a-zA-Z_][a-zA-Z0-9_]*:\n for ([a-zA-Z_][a-zA-Z0-9_]*) in [a-zA-Z_][a-zA-Z0-9_]*:\n print\(\1\) *$ | |
Run an inner loop inside an outer loop. If the inner list contains an element with the value of 1, display "ok". Remember to indent correctly. | for a in b: for c in d: if c == 1: print("ok") | ^ *for [a-zA-Z_][a-zA-Z0-9_]* in [a-zA-Z_][a-zA-Z0-9_]*:\n for ([a-zA-Z_][a-zA-Z0-9_]*) in [a-zA-Z_][a-zA-Z0-9_]*:\n if \1 == 1:\n print\(•ok•\) *$ | |
Run an inner loop inside an outer loop. If the inner list contains an element less than 1, display "ok" and interrupt the loop. Remember to indent correctly. | for a in b: for c in d: if c < 1: print("ok") break | ^ *for [a-zA-Z_][a-zA-Z0-9_]* in [a-zA-Z_][a-zA-Z0-9_]*:\n for ([a-zA-Z_][a-zA-Z0-9_]*) in [a-zA-Z_][a-zA-Z0-9_]*:\n if \1 < 1:\n print\(•ok•\)\n break *$ | |
Code three lines: the first line of an outer loop, then the first line of a loop that runs inside the outer loop, and finally the first line of a loop that runs inside the first inner loop. This is about indenting correctly. | for a in b: for c in d: for e in f: | ^ *for [a-zA-Z_][a-zA-Z0-9_]* in [a-zA-Z_][a-zA-Z0-9_]*:\n for [a-zA-Z_][a-zA-Z0-9_]* in [a-zA-Z_][a-zA-Z0-9_]*:\n for [a-zA-Z_][a-zA-Z0-9_]* in [a-zA-Z_][a-zA-Z0-9_]*: *$ | ^ *14 *$ |
When the looping completes, what number will display? Answer with a numeral. Hint: After the outer loop has run once through one country and the inner loop has run once through four cities, outer_loop_total + inner_loop_total is 5. | outer_loop_total = 0 inner_loop_total = 0 countries = ["Albania", "Morocco", "Brazil", "Denmark"] capitals = ["Tel Aviv", "Abuja", "Brasília", "Islamabad"] for country_to_check in countries: outer_loop_total += 1 for city_to_check in capitals: inner_loop_total += 1 if country_to_check == "Brazil" and city_to_check == "Brasília": print(outer_loop_total + inner_loop_total) | The answer is 14. That's 3 times through the outer loop to get to "Brazil" and 4 + 4 + 3 times through the inner loop to get to "Brasília" | ^ *14 *$ |
| 93bc0f525a | ||
| 86744a925b |