我试图将一个元组列表写入一个文件,然后将它们组织在Value中,从最低到最高,但我总是得到相同的错误。有什么方法可以做到吗?
这是我的代码:
Error: Call to a member function getId() on a non-object
但是,当我运行我的代码时出现错误
name = input("Please enter your name: ")
class_name = input("Which class are you in? ")
score = 0
class_name = class_name + ".txt"
f = open(class_name, 'w')
for t in class_name:
line = ' '.join(str(x) for x in t)
f.write(line + '\n')
f.close()
#See previous scores
print('Please type viewscores() to view the scores')
def viewscores():
viewscore = input("Do you wish to view previous results for your class? ").lower()
if viewscore == 'yes':
viewscore_way = input("a) Alphabetically b)Lowest to highest c)Highest to lowest? ").lower()
elif viewscore != "yes".lower():
print ("Press any key to exit")
ex = input ("")
if viewscore_way == "a".lower():
with open(class_name, 'r') as f:
lines = f.read().splitlines()
print(sorted(lines, key=str.lower))
f.close()
elif viewscore_way == "b".lower():
with open(class_name, 'r') as fp:
lines = [line.strip() for line in fp]
data = [tuple(line.split(',')) for line in lines]
sorted_by_second = sorted(data, key=lambda tup: tup[1])
print(sorted_by_second)
f.close()
elif viewscore_way == "c".lower():
print(sorted(data,key=itemgetter(1), reverse = True))
如何删除超出范围错误'让一切工作?
答案 0 :(得分:0)
您使用空格作为分隔符编写数据,但请以逗号分隔回来。