Suppose I have a for loop:
for i in range(1,10):
if i is 5:
i = 7
I want to change i if it meets certain condition. I tried this but didn’t work.
How do I go about it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For your particular example, this will work:
However, you would probably be better off with a
whileloop:A
forloop assigns a variable (in this casei) to the next element in the list/iterable at the start of each iteration. This means that no matter what you do inside the loop,iwill become the next element. Thewhileloop has no such restriction.