PCEP-30-02 · Question #248
The ABC Company is building a basketball court for its employees to improve company morale. You are creating a Python program that employees can use to keep track of their average score. The program…
The correct answer is C. 1|%-20s 2|%4.1f. Option C works because it correctly matches the data type of each value to the right format specifier. %-20s formats the name as a string (s), with the minus sign (-) forcing left-alignment and 20 setting the minimum field width - padding with spaces on the right if the name is…
Question
- The user name must be left-aligned.
- If the user name has fewer than 20 characters, additional space must be added to the right.
- The average score must have three places to the left of the decimal point and one place to the right of the decimal (xxx.x). What would you insert instead of ??? and ??? ?
Options
- A1 |%-20f 2|%4.1s
- B1|%-20f 2|%4.1
- C1|%-20s 2|%4.1f
- D1|%-20f 2|%1.4s
How the community answered
(28 responses)- A11% (3)
- B4% (1)
- C82% (23)
- D4% (1)
Explanation
Option C works because it correctly matches the data type of each value to the right format specifier. %-20s formats the name as a string (s), with the minus sign (-) forcing left-alignment and 20 setting the minimum field width - padding with spaces on the right if the name is shorter than 20 characters. %4.1f formats the average as a float (f) with one decimal place, producing the xxx.x pattern required.
The distractors all misuse the f specifier on the name variable: options A, B, and D use %-20f, which is for floating-point numbers, not strings - applying it to a name string would cause a TypeError. Option D compounds the error by using %1.4s for the score, which treats a number as a string with a precision of 4 (wrong type entirely). Option B also omits the required type character on the second specifier (%4.1 alone is invalid syntax).
Memory tip: Match the letter to the data - s for strings (names, words), f for floats (decimals, averages); the - sign always means "left" (think of it as pushing content to the left wall).
Community Discussion
No community discussion yet for this question.