나는 (비단뱀) CSV 파일을 인쇄하기 위해이 코드를 작업 중이다. 첫 번째 선택은 잘 작동하고 그것을 알파벳순으로 정렬합니다. 그러나 Choice 2 섹션에서 csv 파일을 가장 높은 점수로 정렬해야합니다. 텍스트/csv 파일 (선택 2에서 잘못된 점은 무엇입니까?
name, score, out of:
Ben,5,20
James,6,20
Adam,12,20
Will,20,20
코드 :
는import operator
import csv
file = open("scores.txt", "r")
scores = csv.reader(file, delimiter=",")
sort = sorted(scores)
for i in range(0, len(sort)):
sort[i].append((max(sort[i][1:2])))
#Alphabetical Order
choice = input("Choice: ")
if choice == "1":
sort = list(sorted(sort,key = operator.itemgetter(0), reverse=False))
print("\nAlphabetical Order:")
print("===================")
for i in range(0, len(sort)):
print("Name: ", sort[i][0], "\tScore: ", sort[i][1])
#Highest score
#sort = sorted(scores)
elif choice == "2":
print("\nHigh Scores:")
print("============")
sort = list(sorted(sort,key = operator.itemgetter(1, 2),reverse=True))
for i in range(0, len(sort)):
print("Name:", sort[i][0], "\tScore:", sort[i][1], "Out of", sort[i][2])
가 is''로 문자열을 비교하지 않습니다. 그건 정체성 때문이야. http://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce –
예, 올바르게 맞았습니다. – BigBang
그게 아마도 큰 문제는 아닙니다. op 코드. 그가'python 3.x'를 사용한다면,'input()'은 문자열을 반환합니다. 그러나 파이썬의 이전 버전을 작업하고있는 경우에는 지적 해 두어야 할 가치가 있습니다. – VHarisop