我正在编写一个程序,读取一个包含国际足联世界杯冠军名单的文本文件,并通过下面显示的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()发布于 2019-04-16 00:13:08
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()发布于 2019-04-16 00:15:39
这是一个缩进问题。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使main在调用它时将f2定义为全局变量而不是局部变量,这将允许您在整个程序中使用它,而不仅仅是在这个函数中。
发布于 2019-04-16 00:13:09
您的代码没有正确缩进,或者您也可以稍微编辑它。
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()https://stackoverflow.com/questions/55698815
复制相似问题