Logi Control Break c для обработки и вывода результатов в Python - PullRequest
0 голосов
/ 30 марта 2020

Я пишу программу, которая будет принимать список дат и цен на газ и возвращать среднее значение за каждый месяц года. Код, который у меня есть, теперь выводит месяц, год и среднее значение в одну строку, но мне нужно, чтобы в этом месяце не было года. Мне просто нужен год до месяцев и средних значений, и я не знаю, как это сделать. Мне сказали использовать контрольную логику прерывания c? Но я никогда не использовал это раньше. Я приложил код, который у меня есть, некоторые из цен на газ и то, что я пытаюсь сделать. Все поможет! Спасибо!

def readGasPrices(fileName):
    gasFile = open(fileName, 'r')
    gasFileList = gasFile.readlines()
    priceList = []
    for line in gasFileList:
        priceList.append(line[11:].rstrip('\n'))
    gasFile.close
    return priceList

def readGasDates(fileName):
    gasFile = open(fileName, 'r')
    gasFileList = gasFile.readlines()
    dateList = []
    for line in gasFileList:
        dateList.append(line[0:10])
    gasFile.close
    return dateList

def average_price_per_month(dates,prices):
    month = ''
    year = ''
    counter = 0
    accumulator = 0
    average_price_months = []
    average_price_prices = []
    average_price_year = []

    for index in range(len(dates)):
        if year == '':
            year = dates[index][6:10]
        elif month == '':
            month = dates[index][0:2]
        elif dates[index][0:2] == month and dates[index][6:10] == year:
            accumulator += float(prices[index])
            counter += 1
        else:
            average = accumulator / counter
            average_price_months.append(month +'-' + year)
            average_price_prices.append(average)
            average_price_year.append(year)
            counter = 1
            accumulator = float(prices[index])
            year = dates[index][6:10]
            month = dates[index][0:2]
    average_price_year = list(dict.fromkeys(average_price_year))
    print()
    print('Month\t\tAverage'.center(35))
    print('------\t\t------'.center(35))

    for index in range(len(average_price_months)):
        print(average_price_months[index].center(30),\
                  format(average_price_prices[index],',.2f').center(9))


def main():
    fileName = input('Enter the file name: ')
    gasPriceList = readGasPrices(fileName)
    dateList = readGasDates(fileName)
    average_price_per_month(dateList, gasPriceList)

Вот как выглядит прайс-лист на газ

Это пример того, что я пытаюсь сделать

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...