nerdexam
Python_Institute

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 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 must allow users to enter their name and current scores. The program will output the user name and the user's average score. The output must meet the following requirements:
  • 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 ??? ?
1 name = input('What is your name?') 2 3 score = 0 4 count = 0 5 while score != -1: 6 score = int(input('Enter your scores: (-1 to end)')) 7 if score == -1: 8 break 9 sum += score 10 count += 1 11 average = sum / count 12 print('???', 'your average score is:', '??? %' (name, average))

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)
  • A
    11% (3)
  • B
    4% (1)
  • C
    82% (23)
  • D
    4% (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.

Full PCEP-30-02 Practice