+ 1

I need help on a python code

I am working on learning concepts, so I started doing a simple story for practice. I am playing around with list functions. I can’t get my code to replace the value of the third item in my list when I print it, and I can’t finish the project until I figure this out. I am a beginner so please try to keep the explanations geared towards that. Thanks! Here’s the code: #guest 3 can’t make it to dinner and I have to invite #someone else instead and send out invitations again guest_list = ["grant cardone", "alexis downing", "jon morales"] message = ", you are invited to dinner at James' house tonight!" guest_1 = guest_list[0].title() + message guest_2 = guest_list[1].title() + message guest_3 = guest_list[2].title() + message guests = guest_1 + " \n" + guest_2 + " \n" + guest_3 print ("I am having a dinner party tonight, and three people are invited to attend.") print ("\nOh no! It looks like " + guest_list[2].title() + " can't make it! Sad day!\n") guest_list[2] = "jack sparrow" print(guests)

27th Apr 2019, 1:25 AM
James Downing
James Downing  - avatar
4 Answers
+ 1
You modify the list, but you print another thing (guests, not guest_list). This will do the trick: print(guest_list) Or if you also want to modify the message just repeat the line "guests = ..." and print it.
27th Apr 2019, 1:48 AM
Diego
Diego - avatar
+ 1
oh ok, thanks! Im reading through a python crash course too. I think i jumped the gun lol
27th Apr 2019, 3:27 AM
James Downing
James Downing  - avatar
0
i thought since guests referenced the index of the individual list that it would change for that variable too. Does it only change at one layer?
27th Apr 2019, 2:21 AM
James Downing
James Downing  - avatar
0
They're different objects, so changing one doesn't change the other. The case where they are the same object is explained in the Python course.
27th Apr 2019, 2:44 AM
Diego
Diego - avatar