I am attempting to trace the execution of a piece of code that contains a for loop with two if conditionals. But I need help understanding exactly how for loops are executed in python.
Please consider the following example:
numAs = 0
numEs = 0
aStr1 = 'abcdefge'
def someFunc(aString):
1. for i in range(len(aString)):
2. if s[i] == 'a':
3. numAs += 1
4. continue
5. if s[i] == 'e':
6. numEs += 1
7. if numEs > numAs:
8. break
9. print(someFunc(aStr1))
Question: Using aStr as a parameter how many times will line 1 execute in the above code?
My understanding is that line 1. of this piece of code: for i in range(len(aString)), will only be executed once. While lines 2 and 5 will be executed multiple times depending on the string that is passed. When the function encounters the continue statement it goes back to line 2 and runs it. Please confirm or correct my thinking.
Thanks
continuestatement have you read? Can you provide a link or a reference to the tutorial you're using? It's not clear what you've read, so it's not clear what you're really asking.for c in aString. Right now you are creating a list of n integers (n being the length of the string, retrieving integers from that list one at a time, and using them to index into a string, when you could just iterate directly over the characters of the string.