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
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)- A5% (2)
- B71% (27)
- C16% (6)
- D8% (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.