简体   繁体   中英

World cup Python progam

I am working on a program that reads a text file containing a list of FIFA World Cup champions and determines, by the world_cup_champions.txt file shown below, the country that has won the most championships display the countries alphabetically. I am getting this error message:

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

Here's my code:

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 is defined inside def main() which makes it local variable, when you call it in for l in f2 the f2 is outside the function so you need global variable to be called. If you really going to put everything inside main(), than you have to indent everything like this

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

This is an indentation issue. f2 is being defined within main , meaning it cannot be used outside of main . You attempted to use it in the for l in f2 loop, which resulted in your NameError .

Your main function only has one statement, f2 = open("world_cup_champions.txt","r+") , so you probably meant to define your program like this:

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

If you just wanted that one statement in main , but you wanted to use f2 throughout the program, you could define main like this:

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

The first statement, global f2 , makes main define f2 as a global variable rather than a local variable when it's called, which will allow you to use it throughout the program rather than just in that one function.

Your code isn't indented correctly, alternatively you can edit it slightly

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

alternatively, just do this

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

You indentation is off. You need to have all statements inline with the first one.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM