#python #index-error
#python #ошибка индекса
Вопрос:
file=open("taxy.txt","r")
append=open("taxy.txt","a")
print("Do you want to hire or rent a car?")
choice=input("Click 1 to hire and 2 for renting")
if choice=="1":
car_list = []
price_list = []
specification = []
for lines in file:
lines = lines.rstrip()
temp_list = lines.split()
car_list.append(temp_list[0])
price_list.append(temp_list[1])
temp_specification = ""
for texts in range(2, len(temp_list)):
temp_specification = " " temp_list[texts]
specification.append(temp_specification)
car=input('Name of car you want to hire')
if car not in car_list:
print("This car is not available right now")
У меня есть текстовый файл со спецификациями, касающимися автомобилей, и их спецификациями. Ошибка находится в строке car_list.append(temp_list[0])
Файл «C:UserscrammOneDriveDesktopUniAssignmentsBN108taxy.py «, строка 20, в car_list.append(temp_list[0])Является ли сообщение об ошибке вместе с IndexError: индекс списка вне диапазона
Комментарии:
1. похоже
temp_list = lines.split()
, что возвращен пустой список, поэтомуcar_list.append(temp_list[0])
увеличено исключение.2. Есть ли вероятность, что у вас была пустая строка в файле?
3. @balderman Но мой список должен быть полным или, по крайней мере, содержать содержимое, основанное на текстовом файле no?
4. @Booboo привет, это была проблема, спасибо
5. @CramanMishra добавьте
print(len(temp_list))
и посмотрите, пусто ли оно илиNone