Мне нужно определить среднюю функцию
Определить основную функцию, которая будет включать в себя следующие вещи
- запрашивать у пользователя ввод имени текстового файла
- открыть и прочитать входной файл, можно сделать до или внутри high функции заказа
- использовать две функции высшего порядка
- вызвать главную
- выйти из программы
Код: Выделить всё
'Write a program that computes and prints the average of numbers in a text file.'
def getNumberList(filename):
f = open(filename,'r')
line = f.readline()
numbers = line.split(',')#split the numbers separated by comma
numberList = []#holds the integer value
for i in numbers:
numberList.append(int(i))
return numberList
def getAverage(numbers):
sum = 0#stores the sum of the numbers in the list
count = 0#keeps the count of numbers in the list
for i in numbers:
sum = sum + i
count = count + 1
average = sum/count#calculate the average
return average
def main():
#take input from the user
filename = input("Enter filename : ")
#get the numbers from the file
numbers = getNumberList(filename)
#get the average from the numbers list
average = getAverage(numbers)
#display the average
print(average)
if __name__ == "__main__":
main()
Подробнее здесь: https://stackoverflow.com/questions/628 ... -text-file
Мобильная версия