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
This set of exercises reviews some things you learned in previous chapters. Code the first line of an if statement testing whether the value stored in x is greater or equal to the value stored in y. | if x >= y: | ^ *if x >= y: *$ | |
In a single statement, display the remainder when 9 is divided by 4. | print(9 % 4) | ^ *print\(9 % 4\) *$ | |
Code the shorthand version of this code: | total = total - discount | total -= discount | total `-`= `discount |
x equals 3 times 4 divided by the sum of 5 plus 3. Code it to eliminate ambiguity. | x = (3 * 4) / (5 + 3) | x = (3 * 4) / (5 + 3) | |
Concatenate the strings stored in two variables and assign the combination to a third variable. Make up all the variable names. | whole_thing = first_part + second_part | ^ *[a-zA-Z_][a-zA-Z0-9_]* = [a-zA-Z_][a-zA-Z0-9_]* \+ [a-zA-Z_][a-zA-Z0-9_]* *$ | |
Write the first line that tests whether a equals b and either c is greater than d or e is less than f. Use a pair of parentheses to eliminate ambiguity. | if a == b and (c > d or e < f): | ^ *if a == b and \(c > d or e < f\): *$ | |
In a single line, display "yellowtail" by specifying the list name and index number. | fishes = ["cod", "trout", "yellowtail", "tuna", "salmon"] | print(fishes[2]) | ^ *print\(fishes\[2\]\) *$ |
In a single line, add two elements to a list. The elements are numbers. Make up the numbers and the list name. | x = x + [87, 544] | ^ *([a-zA-Z_][a-zA-Z0-9_]*) = \1 \+ \[[-]?(?:[.]\d+|\d+(?:[.]\d*)?), [-]?(?:[.]\d+|\d+(?:[.]\d*)?)\] *$ | |
Slice "tablet" and "smart watch" from this list and assign the slice to the variable piece. | devices = ["computer", "smart phone", "tablet", "smart watch", "video game console"] | piece = devices[2:4] | ^ *piece = devices\[2:4\] *$ |
Eliminate "computer" by specifying the element's value. | devices = ["computer", "smart phone", "tablet", "smart watch", "video game console"] | devices.remove("computer") | ^ *devices\.remove\(•computer•\) *$ |
| 446cc061c2 | ||
| 5ea9ce67f8 |