May-18-2019, 03:23 PM
So I understand that there are better ways of doing this. I figuered out a couple ways but I am still perplexed as to why this didn't work. I am getting an "IndexError: list index out of range" but I can't figure out why.
Input:
-Josh
Yay! I figured it out all by myself! The loop should not have been >= because the last element in the list will be numbered one less than the actual count of elements.
The corect loop is
Input:
guest_list = ['Guest 1', 'Guest 2', 'Guest 3', 'Guest 4', "Guest 5"] guest_list_len = (len(guest_list)) x=0 while x <= guest_list_len: print (guest_list[x] + ", you are invited to a party!") print (str(x), len(guest_list)) x += 1Output:
Output:Traceback (most recent call last):
Guest 1, you are invited to a party!
0 5
File "C:/Users/xxxxxxx/PycharmProjects/lists/guest_list.py", line 10, in <module>
Guest 2, you are invited to a party!
print (guest_list[x] + ", you are invited to a party!")
1 5
IndexError: list index out of range
Guest 3, you are invited to a party!
2 5
Guest 4, you are invited to a party!
3 5
Guest 5, you are invited to a party!
4 5
Process finished with exit code 1
As you can see, none of my list indexs are out of range. Thanks for your help!-Josh
Yay! I figured it out all by myself! The loop should not have been >= because the last element in the list will be numbered one less than the actual count of elements.
The corect loop is
while x < guest_list_len: