Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Skipping Loop
#1
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
Reply
#2
please put your code in the [python] tags so we can see how you indented it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#3
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.
Reply
#4
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
Reply
#5
Thank you very much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File loop curiously skipping files - FIXED mbk34 10 685 Feb-10-2024, 07:08 AM
Last Post: buran
  Skipping line in text without Restarting Loop IdMineThat 4 1,433 Apr-05-2022, 04:23 AM
Last Post: deanhystad
  python seems to be skipping lines of code alansandbucket 1 4,088 Jun-22-2021, 01:18 AM
Last Post: Larz60+
  Skipping Strings Kristenl2784 0 1,463 Jul-27-2020, 06:01 PM
Last Post: Kristenl2784
  looping for time while skipping a day Staph 2 2,142 Jun-23-2019, 03:33 AM
Last Post: Staph
  When I read csv file i am getting b'Skipping line messages GuJu 4 10,279 Mar-07-2019, 11:47 AM
Last Post: GuJu
  For loop question(skipping) jure98 1 2,435 Mar-31-2018, 01:54 PM
Last Post: Larz60+
  Game inputs skipping Azazel 2 3,916 May-25-2017, 02:29 PM
Last Post: Azazel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020