nerdexam
Python_Institute

PCEP-30-02 · Question #144

You have the following le. index.py: from sys import argv\nsum = 0\nfor i in range(2, len(argv)):\n sum += float(argv[i])\nprint(\n \"The average score for {0} is {1:.2f}\"\n .format(argv[1]…

The correct answer is B. python index.py Peter 100 200 300. Option B is correct because passing 100 200 300 as scores gives a sum of 600 divided by 3 scores (len(argv)-2 = 5-2 = 3), which equals exactly 200.00 - matching the required output with "Peter" as argv[1]. Option A fails because a single score of 100 divided by 1 yields 100.00…

Question

You have the following le. index.py: from sys import argv\nsum = 0\nfor i in range(2, len(argv)):\n sum += float(argv[i])\nprint(\n "The average score for {0} is {1:.2f}"\n .format(argv[1], sum/(len(argv)-2))\n)\nYou want the following output: The average score for Peter is 200.00. Which command do you have to execute in the command line?

Options

  • Apython index.py Peter 100
  • Bpython index.py Peter 100 200 300
  • CThe code is erroneous.
  • Dpython index.py Peter 100 200

How the community answered

(38 responses)
  • A
    5% (2)
  • B
    71% (27)
  • C
    16% (6)
  • D
    8% (3)

Explanation

Option B is correct because passing 100 200 300 as scores gives a sum of 600 divided by 3 scores (len(argv)-2 = 5-2 = 3), which equals exactly 200.00 - matching the required output with "Peter" as argv[1].

Option A fails because a single score of 100 divided by 1 yields 100.00, not 200.00. Option D fails because (100+200)/2 = 150.00, still not 200.00. Option C is wrong because the code is valid Python that runs without errors.

Memory tip: Remember that argv[0] is always the script name, so scores start at index 2 - count your arguments carefully by working backwards from the desired average to find what inputs produce it.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice