世界杯Python Progam

时间:2019-04-16 00:07:49

标签: python

我正在开发一个程序,该程序读取包含FIFA世界杯冠军名单的文本文件,并通过下面显示的world_cup_champions.txt文件确定获胜最多的国家按字母顺序显示国家。我收到此错误消息:

Traceback (most recent call last):
  File "D:\CUP.py", line 8, in <module>
    for l in f2:
NameError: name 'f2' is not defined.

这是我的代码:

def main():
    f2  = open("world_cup_champions.txt","r+")

dict_values ={}
temp_list = []
tmp_list2 = []

for l in f2:
    temp_list.append(l.strip())
    temp_list = temp_list[1:]


for val in temp_list:
    tmp_val = val.split(',')
    if tmp_val[1] not in dict_values:
        dict_values[tmp_val[1]] = 1
else:
    dict_values[tmp_val[1]] += 1

for key,value in dict_values.items():
    tmp_list2.append([key, value])


    tmp_list2.sort(key=lambda x: x[0])
    print(" Country " + " Wins " + "Years")
for val in tmp_list2:
    print(" " + val[0] + " " + str(val[1]))

if __name__ == "__main__":
    main()

4 个答案:

答案 0 :(得分:1)

f2定义在def main()内部,这使其成为局部变量,当您在for l in f2中调用时,f2在函数外部,因此需要全局变量被调用。如果您真的要将所有内容都放入main()内,那么您必须缩进这样的内容

def main():
    f2  = open("world_cup_champions.txt","r+")

    dict_values ={}
    temp_list = []
    tmp_list2 = []

    for l in f2:
        temp_list.append(l.strip())
        temp_list = temp_list[1:]


    for val in temp_list:
        tmp_val = val.split(',')
        if tmp_val[1] not in dict_values:
            dict_values[tmp_val[1]] = 1
    else:
        dict_values[tmp_val[1]] += 1

    for key,value in dict_values.items():
        tmp_list2.append([key, value])


        tmp_list2.sort(key=lambda x: x[0])
        print(" Country " + " Wins " + "Years")
    for val in tmp_list2:
        print(" " + val[0] + " " + str(val[1]))

if __name__ == "__main__":
    main()

答案 1 :(得分:1)

这是一个缩进问题。 f2是在main中定义的,这意味着它不能在main之外使用。您尝试在for l in f2循环中使用它,从而导致NameError

您的main函数只有一个语句f2 = open("world_cup_champions.txt","r+"),因此您可能打算像这样定义程序:

def main():
    f2  = open("world_cup_champions.txt","r+")

    dict_values ={}
    temp_list = []
    tmp_list2 = []

    for l in f2:
        temp_list.append(l.strip())
        temp_list = temp_list[1:]


    for val in temp_list:
        tmp_val = val.split(',')
        if tmp_val[1] not in dict_values:
            dict_values[tmp_val[1]] = 1
        else:
            dict_values[tmp_val[1]] += 1

    for key,value in dict_values.items():
        tmp_list2.append([key, value])


        tmp_list2.sort(key=lambda x: x[0])
        print(" Country " + " Wins " + "Years")
    for val in tmp_list2:
        print(" " + val[0] + " " + str(val[1]))
if __name__ == "__main__":
    main()

如果您只想在main中使用一条语句,但是想在整个程序中使用f2,则可以这样定义main

def main():
    global f2
    f2 = open("world_cup_champions.txt","r+")

第一个语句global f2使mainf2定义为全局变量而不是 local变量调用,这样您就可以在整个程序中使用它,而不仅仅是在一个函数中使用它。

答案 2 :(得分:0)

您的代码缩进不正确,或者您可以对其稍加修改

def main():
    f2  = open("world_cup_champions.txt","r+")

    dict_values ={}
    temp_list = []
    tmp_list2 = []

    for l in f2:
        temp_list.append(l.strip())
        temp_list = temp_list[1:]


    for val in temp_list:
        tmp_val = val.split(',')
        if tmp_val[1] not in dict_values:
            dict_values[tmp_val[1]] = 1
    else:
        dict_values[tmp_val[1]] += 1

    for key,value in dict_values.items():
        tmp_list2.append([key, value])

    tmp_list2.sort(key=lambda x: x[0])
    print(" Country " + " Wins " + "Years")
    for val in tmp_list2:
        print(" " + val[0] + " " + str(val[1]))

if __name__ == "__main__":
    main()

或者,只要这样做

def main():
    return open("world_cup_champions.txt","r+")

dict_values ={}
temp_list = []
tmp_list2 = []

f2 = main()

for l in f2:
    temp_list.append(l.strip())
    temp_list = temp_list[1:]


for val in temp_list:
    tmp_val = val.split(',')
    if tmp_val[1] not in dict_values:
        dict_values[tmp_val[1]] = 1
else:
    dict_values[tmp_val[1]] += 1

for key,value in dict_values.items():
    tmp_list2.append([key, value])


    tmp_list2.sort(key=lambda x: x[0])
    print(" Country " + " Wins " + "Years")
for val in tmp_list2:
    print(" " + val[0] + " " + str(val[1]))

if __name__ == "__main__":
    main()

答案 3 :(得分:-1)

您的缩进已关闭。您需要使所有语句与第一个语句内联。

def main():
   f2  = open("world_cup_champions.txt","r+")
   dict_values ={}
   temp_list = []
   tmp_list2 = []