簡體   English   中英

世界杯Python Progam

[英]World cup Python progam

我正在開發一個程序,該程序讀取包含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()

f2定義內def main()這使得它的局部變量,當你調用它for l in f2f2是功能之外,所以你需要全局變量來進行調用。 如果您真的要將所有內容都放入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()

這是一個縮進問題。 f2被內定義main ,這意味着它不能成為使用外部main 您試圖for l in f2中的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使main定義f2將其定義為全局變量而不是局部變量 ,這將使您可以在整個程序中使用它,而不僅僅是在一個函數中使用它。

您的代碼縮進不正確,或者您可以對其稍加編輯

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()

您的縮進已關閉。 您需要使所有語句與第一個語句內聯。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM