“for loop” with two variables?

If you want the effect of a nested for loop, use: If you just want to loop simultaneously, use: Note that if x and y are not the same length, zip will truncate to the shortest list. As @abarnert pointed out, if you don’t want to truncate to the shortest list, you could use itertools.zip_longest. UPDATE Based on the request for “a …

Read more

How do I convert this for loop into a while loop?

The general structure of a basic for statement is: ForInit is the initializer. It is run first to set up variables etc. Expression is a boolean condition to check to see if Statement should be run Statement is the block of code to be run if Expression is true ForUpdate is run after the Statement to e.g. update variables as necessary After ForUpdate has been run, Expression is evaluated …

Read more

List append() in for loop

The list.append function does not return any value(but None), it just adds the value to the list you are using to call that method. In the first loop round you will assign None (because the no-return of append) to a, then in the second round it will try to call a.append, as a is None …

Read more

break statement in “if else” – java

Because your else isn’t attached to anything. The if without braces only encompasses the single statement that immediately follows it. Not using braces is generally viewed as a bad practice because it can lead to the exact problems you encountered. In addition, using a switch here would make more sense. Note that instead of an infinite for loop I used a while(boolean), making it …

Read more