21.7k views
4 votes
Write a program that will read a file (data.txt). The file contains integer values. The program will read the file and create a list.

Using list methods/functions, the program will display the numbers, the count of numbers, the sum of the numbers, the average of the numbers, the maximum integer, and the minimum of the numbers. (LOOK AT PICTURE BELOW PLS, PYTHON)

Write a program that will read a file (data.txt). The file contains integer values-example-1
User MuhKuh
by
7.6k points

1 Answer

3 votes

Answer:

def readData(filename):

# Open the file and read the contents

with open(filename, 'r') as file:

contents = file.read()

# Convert the contents to a list of integers

numbers = list(map(int, contents.split()))

# Return the list of numbers

return numbers

def main():

# Call the readData function to get the list of numbers

numbers = readData('data.txt')

# Print the numbers

print("Numbers: ", numbers)

# Print the count of numbers

print("Count: ", len(numbers))

# Print the sum of numbers

print("Sum: ", sum(numbers))

# Print the average of numbers

print("Average: ", sum(numbers)/len(numbers))

# Print the maximum integer

print("Max: ", max(numbers))

# Print the minimum integer

print("Min: ", min(numbers))

if __name__ == "__main__":

main()

User Cybergen
by
7.6k points