程序中的Python代码错误应该计算出Google的月平均价格,并告诉我们Google的最佳和最差的6个月期间

2024-05-10 01:09:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要帮助来找出如何修复Python代码分配中的错误。 基本上,我需要创建一个程序,应该计算谷歌的月平均价格,告诉我们最好和最差的六个月期间谷歌。 平均价格定义为((v1*c1)+(v2*c2)+(v3*c3)+(v4*c4)…+(vn*cn))/(v1+v2+v3+v4…+vn),其中vi是第一天的成交量,ci是第一天的调整收盘价。 对于这项任务,我们将从2004年到2012年考察谷歌。我们需要的数据集是一个CSV格式的表,可以从https://www.dropbox.com/s/qr7cu2kui654kgz/googlePrices.csv下载。在

到目前为止,我已经创建了下面的Python代码,但是它显示了一些错误消息,我无法理解。在

提前谢谢你。在

谨致问候, 罗伯托·莱尔

在============================================================ 代码:

__author__ = 'Roberto'

def get_values():

import operator

data_file = open("googlePrices.csv")


def get_data_list(file_name):
    data_list = []  # always start with an open list
    for line_str in data_file:
        data_list.append(line_str.strip().split(','))  # removes all commas from the csv file
    return data_list


def monthlyAverage(list_of_tuples=[]):


    averageList = []
    currentYear_int = 2012
    month_int = 11
    sum_float = float()
    count = 0

    for a_tuple in list_of_tuples:
        dateStr = a_tuple[0]
        Floatdata = a_tuple[1]
        listdate = dateStr.split(',')
        Year_int = int(listdate[0])
        month_int = int(listdate[1])
        date_year_str = "Date: " + str(month_int) + "-" + str(currentYear_int)

    if month_int != currentYear_int:
        float_average = sum_float / count
        list_average = [date_year_str , float_average]
        average_tuple = tuple(list_average)
        averageList.append(average_tuple)
        current_month_int = month_int
        sum_float = 0.0
        count = 0
        sum_float += Floatdata
        count += 1

    return averageList


def best_6_month(averageList):
    averageList.sort()
    print("The 6 best months fo google are: ")
    print()
    print(averageList)


def worst_6_month(averageList):
    averageList.reverse()
    print("The 6 worst months for google are: ")
    print()
    print(averageList)


optionStr = ""

if optionStr != "0":
    print("------------------------------")
    print("\n Google Stock Prices. 2004 - 2012")
    print("------------------------------")
    print("Please choose an option")
    print()
    print("1.    Calculate Average")
    print("2.    View best six months")
    print("3.    View worst six months")
    print("0.    Exit program")
    print()

optionStr = input("Enter you choice now: ")
if (len(optionStr) != 1) or (optionStr not in "0123"):
    print("You have entered an invalid number!")
    print("Please try again")

elif optionStr == "1":
    monthlyAverage()

elif optionStr == "2":
    best_6_month()

elif optionStr == "3":
    worst_6_month()

elif optionStr == "0":
    print("\n Exiting program... Goodbye.")

else:
    print("\n Bad option")

====================================================================

^{pr2}$

Tags: datadefcountfloatlistfileintsum