MUST BE IN PYTHON!!! If someone helps with this in the next 6 hours. I will incr
ID: 638454 • Letter: M
Question
MUST BE IN PYTHON!!! If someone helps with this in the next 6 hours. I will increase the points to MAX. I really need help with this. Thanks!!!
13.3 (Process scores in a text file) Suppose that a text file contains an unspecified number of scores. Write a program that reads the scores from the file and displays their total and average. Scores are separated by blanks. Your program should prompt the user to enter a filename.
OUTPUT:
13.3Process scores in a text file) Suppose that a text file contains an unspecified nu ber of scores. Write a program that reads the scores from the file and displays their total and average. Scores are separated by blanks. Your program should prompt the user to enter a filename. Here is a sample run: Enter a filename: scores.txt There are 70 scores The total is 800 The average is 33.33Explanation / Answer
# function to convert list of strings to real numbers
def toNumbers(nums):
for i in range(len(nums)):
nums[i] = int(nums[i])
# function to square the numbers in a list
def countNums(nums):
count = 0
for i in range(len(nums)):
count = count + 1
return count
# function to add the numbers in the list
def sumList(nums):
total = 0
for i in nums:
total = total + i
return total
# program that pulls numbers from file and computes sum of the square of the numbers
def main():
fname = input("Please enter the name of the file to be opened: ")
nums = open(fname, 'r')
strings = ""
#create a string rather than a list with readlines
#so we can remove extra spaces with the .split() method later
for line in nums.readlines():
strings += line
#make sure to close nums
nums.close()
#remove spaces and store this result in the list nums_array
nums_array = strings.split()
print("The numbers in this list are:")
print(nums_array)
# Convert strings in list to actual numbers
toNumbers(nums_array)
# Counting total no of elements
count =countNums(nums_array)
total = sumList(nums_array)
avg = total/count
print
print("The number of elements is: " + str(count))
print("The sum of the numbers from this list is: " + str(total))
print("The average of the numbers from this list is: " + str(avg))
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.