Python noob; please explain why this loop doesn’t exit.
for i in range(0,10):
print "Hello, World!"
if i == 5: i = 15
print i
next
regards
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.
You are iterating over a
rangeobject, which is just a collection of values ranging from0...10. There is no logic behind it, as it is just a collection of numbers.The
for item in setsyntax essentially just loops over every element in theset, breaking only when every item has been looped over.Your code is not equivalent to a C++
forloop, which would produce your desired result:If you want to break out of the loop, just
breakthe loop: