1

I am trying to have the user input delivery information and use the for loop. I keep getting an IndexError: list assignment index out of range

I want it to print and have the user input each item then after ask if info is correct and if not return them. However, I cannot get to for statement correct after trying multiple ways. Thank you.

address = []
address_info = ('Name:', 'Address:', 'City:', 'State', 'Zip Code')

print('***DELIVERY METHOD***')
print('Please enter the info below from customer...')
for x in address_info:
    address[x] = input(address_info[x])
    x += 1

1 Answers1

-1

Try using append-logic instead of using indexing.

address = []
address_info = ('Name:', 'Address:', 'City:', 'State', 'Zip Code')

print('***DELIVERY METHOD***')
print('Please enter the info below from customer...')

for info in address_info:     # these are the values not indicies
    user_input = input(info)
    address.append(user_input)

There were to issues in your original code. First, the for-loop will loop over the values from address_info rather than the indicies. Second, the result list needs to be grown by appends; you can't make an indexed assignment until the list already has some value at the the position that can be replaced.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485