当前位置:首页 > Python语言程序设计(美-梁勇)第4章习题解答(英文)
Chapter 4 Selections
1. 2. Yes. i becomes 1, j becomes 0, b1 becomes True, and b2 becomes Flase.
3. random.randrange(0, 20) or random.randint(0, 19)
4. 5. 6. 7. 8.
if score > 90: pay *= 1.03
<, <=, ==, !=, >, >=
random.randrange(10, 20) or random.randint(10, 19) random.randrange(10, 50 + 1) or random.randint(10, 50) random.randrange(0, 2) or random.randint(0, 1)
if y > 0: x = 1
9.
if score > 90: pay *= 1.03 else:
pay *= 1.01
10.
If number is 30, (a) displays 30 is even 30 is odd
(b) displays 30 is even
If number is 35, (a) displays 35 is odd
(b) displays
35 is odd 11. Note: else matches the second if clause. The output is “x is 3” if x = 3 and y = 2. The output is “z is 7” if if x = 3 and y = 4. No output if if x = 2 and y = 2.
True x > 2 False y > 2 True z = x + y print(\ False print(\
12. Note: else matches the first if clause. The output is “x is 2” if x = 2 and y = 4. No output if if x = 3 and y = 2. The output is “z is 6” if if x = 3 and y = 3.
True x > 2 False y > 2 True z = x + y print(\ False print(\ 13.
Consider score is 90, what will be the grade? The conditions are tested in the wrong orders.
14.
(A) and (C) are equivalent. (B) and (D) are incorrectly indented.
newLine = (count % 10 == 0)
15.
16.
Both are correct. (B) is better. Both conditions in (A) are tested. In (B) the condition is
tested only once.
17.
For number is 14, (a) displays: 14 is even
(b) displays 14 is even
For number is 15, (a) displays: 15 is multiple of 5
(b) displays
15 is multiple of 5
For number is 30, (a) displays: 30 is even
30 is multiple of 5
(b) displays 30 is even
18.
Yes
19. 20.
This program will have a runtime error, because tax will not be created. (true) and (3 > 4) False
not(x > 0) and (x > 0)
False
(x > 0) or (x < 0) True
(x != 0) or (x == 0) True
(x >= 0) or (x < 0) True
(x != 1) == not (x == 1)
True
21. (x > 1) and (x < 100)
22. ((x > 1) and (x < 100)) or (x < 0) 23.
x >= y >= 0 False
x <= y >= 0 True
x != y == 5 True
(x != 0) or (x == 0) True
24.
Yes
25.
If ch is 'A', the expression is true; If ch is 'p', the expression is false; If ch is 'E', the expression is true; If ch is '5', the expression is false;
26.
(x < y and y < z) is True (x < y or y < z) is True not (x < y) is False (x < y < z) is True
not (x < y < z) is False
27. age > 13 and age < 18 28.
weight > 50 or height > 160.
共分享92篇相关文档