USING PYTHON 3 There are errors in the code shown. List and describe each error,
ID: 3858927 • Letter: U
Question
USING PYTHON 3
There are errors in the code shown. List and describe each error, and provide a correction.
import sys
if len(argv) >=3:
name = argv[0]
scores = argv[2:]
for ct in range(len(scores)):
scores[ct] += 10
print(scores)
else:
print("invalid # of arguments")
==========================
Write a Python program that accepts the number of scores followed by that many scores via the command line and prints the average of the scores. Include appropriate error-checking to make sure command line arguments are provided. No other error checking is required.
Example1: python calcAvg.py 5 100 90 70 80 90
Output of the code should be: Average: 87
Example2: python calcAvg.py 3 100 90 80
Output of the code should be: Average: 90
Example 3: python calcAvg.py
Output of the code should be: No scores were provided – average cannot be calculated.
Explanation / Answer
import sys
if len(sys.argv) >=3:
name = sys.argv[0]
scores = sys.argv[2:]
for ct in range(len(scores)):
scores[ct] = int(scores[ct])
if len(scores) == int(sys.argv[1]):
print("Average", float(sum(scores)/len(scores)))
else:
print("No scores were provided average cannot be calculated.")
else:
print("invalid # of arguments")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.