두 개의 텍스트 파일을 참조하는 프로그램을 작성하고 있습니다. 하나는 문장의 통화 목록을 저장하고 하나는 현재 값을 저장합니다. 문장의 통화 값을 한 번만 저장하고 각 주문을 저장하여 화면에서 바꿀 수 있도록하고 싶습니다. 예 : 스털링, 유로 (하나의 파일로). 1,1,2 (주문 파일에서). 당신이 주로 먼저 통화를 색인하고 있기 때문에 당신은 오류가 발생하는 범위IndexError : 목록 색인이 범위를 벗어남 - 파이썬에서 출력을 저장하기 위해 txt 파일을 가져올 수 없습니다.
all_currency = []
value_of_currency = []
my_words_file = open("words.txt","r")
my_words_document = my_words_file.readline()
while my_words_document != "":
all_currency.append(my_words_document.strip())
my_words_document = my_words_file.readline()
print("output of the words in the saved list:",str(all_currency))
my_words_file.close()
positions = ""
my_numbers_file = open("numbers.txt","r")
value_of_currency = my_numbers_file.readline().strip().split()
my_numbers_file.close()
for mynumbers in value_of_currency:
positions += all_currency[int(mynumbers)]+" "
print("output of the positions in the saved list:",positions)
IndexError는 어느 라인에서 켜져 있습니까? 나는 그것의'position + = all_currency [int (mynumbers)] + ""''그 줄 앞에'int (mynumbers)'를 출력하고 그것이 무엇을 말하는지 보려고합니다. – Jeff
오류의 흔적을 보여 주실 수 있습니까? –
네, 그 줄입니다. –