Python Forum

Full Version: Skipping Loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Gents,

I could not do Step 5 in this Skipping loop exercise, for step 5, where do I put the codes thanks.

1)Start a new program with a statement creating a loop that iterates three times
for i in range( 1, 4 ) :
2)Next, add an indented statement creating a “nested” inner loop that also iterates three times
for j in range( 1, 4 ) :
3)Now, add a further-indented statement in the inner loop to display the counter
numbers (of both the outer loop and the inner loop) on each iteration of the inner loop
print( ‘Running i=’ + i + ‘ j=’ + j )
4)Save then run this program – to see the counter values on each loop iteration

5)Insert a break statement at the start of the inner loop to break from that loop – then run the
program again
if i == 2 and j == 1 :
print( ‘Breaks inner loop at i=2 j=1’ )
break

Code above in python tags
for i in range( 1, 4 ) :
   for j in range( 1, 4 ) :
      print( ‘Running i=’ + i + ‘ j=’ + j )
         if i == 2 and j == 1 :
            print( ‘Breaks inner loop at i=2 j=1’ ) 
            break
please put your code in the [python] tags so we can see how you indented it.
When I ran the code, it gave me this Error:

SyntaxError: Non-UTF-8 code starting with '\x91' in file loop2.py on line 3, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details.

Can somebody help me with this, thanks.
try changing the ‘’ to ''
and so you dont get
Error:
print('Running i=' + i + ' j=' + j) TypeError: can only concatenate str (not "int") to str
Use the new string formating
for i in range(1, 4) :
    for j in range(1, 4) :
        print(f'Running i= {i}  j= {j}')
        if i == 2 and j == 1 :
            print('Breaks inner loop at i=2 j=1') 
            break
Thank you very much.