Hey, I was wondering how exactly this code worked for this problem? I was trying to make a while loop using variables so the first letter would move back to the last, then the second would move to the first and vice versa but that loop didn't work out. So I got this code from a friend who did the question but I couldn't understand how their code works
#🔒 hw help
26 messages · Page 1 of 1 (latest)
@torpid ivy
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
If you guys could, I would also love to know how to make a loop work for this
I was doing something like this, this is obviously incomplete but I wasn't sure how to go ahead with this
I'm typing out the originals so it's easier to talk about them, OK?
def cycle(input_list):
tmp = input_list[0]
length = len(input_list)
for i in range(length-1):
input_list[i] = input_list[i+1]
input_list[-1] = tmp
ok
the way this works is that it first saves the first item in the list in a temp variable (tmp = input_list[0])
then it loops through the numbers 0 through the last index of the list
and for each, it takes the next list item and puts it in the current one
so when i is 1, it takes the value in input_list[2] and puts it in input_list[1]
i thought it took the first item and moved it to the second
it makes so much more sense now
thanks
why is there an index error for this
when b = 4, it’s at the last index of the list
but you compared it with input_list[b+1]
so it’s comparing with a number outside of the list
so index error
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.