Python Forum
adding to code but still getting same output
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
adding to code but still getting same output
#1
Hello all,
I am a total newb and I am using a self study guide with interactive labs in the book using the most current version of Python (3.6.1). I've been doing pretty well and have even experimented and found small fixes to the example code to make my output match the examples in the book, I.E. adding / removing a [space] between \t  within a print statement, etc. very small things but big for a newb like myself. I am working on the chapter about loops and have followed the example... from what I can see exactly as it's written, but when I add a break command statement and then the continue to the existing loop code, I'm getting the exact same result and despite my looking at it... I can't seem to figure out why or what I am missing. I will admit that the instruction to add the break command was very slightly more vague than all of the previous, very concise instructions, but I have messed around with it and tried every different spacing combination I can think of and I either get a syntax error or the same output.
In the grand scheme of things this is a very small issue, but I'll be honest, it's bugging the crap out of me that I can't see or figure out what I've done wrong or am missing. I have had some errors along the way but have always up to this point been able to find the missing [ ' ] [ , ] [ ) ] or whatever mistake I've made in my code... I can't find this one and it's driving me nuts. :D

Here is the loop code example

for i in range(1,4):
   for j in range(1,4):
       print( 'Running i =', i, 'j =', j )
and here is the matching output I get for the above code

Output:
Running i = 1 j = 1 Running i = 1 j = 2 Running i = 1 j = 3 Running i = 2 j = 1 Running i = 2 j = 2 Running i = 2 j = 3 Running i = 3 j = 1 Running i = 3 j = 2 Running i = 3 j = 3
Then I add the break statement as instructed like this

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
At this point I am supposed to get a similar output as above but with a new line of text in the middle saying

'Breaks inner loop at i=2 j=1'

Plus the whole i=2 output is supposed to be gone

but instead I get the exact same output as the nested loop code. and same thing with the continue command, it gives me the same output as above so I'm confused.

Any insights from any more experienced coders out there would be greatly appreciated!

Best regards,  Smile
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#2
check your indentation. The if block is outside the second loop. so it is executed only after the loop has finished. and because at that point j is always equal to 3, nothing is printed.
Reply
#3
That's kinda where I had trouble because the example only said to add it, it didn't exactly say where. However I have tried changing the indent on the if statement several times and anywhere other than where it is in my example and I get a syntax error.

Thank you for your assistance Smile
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#4
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
Output:
Running i = 1 j = 1 Running i = 1 j = 2 Running i = 1 j = 3 Running i = 2 j = 1 Breaks inner loop at i=2 j=1 Running i = 3 j = 1 Running i = 3 j = 2 Running i = 3 j = 3
Also, note that it is better to us string formatting, i.e. replace

print( 'Running i =', i, 'j =', j )
with

print('Running i = {}, j = {}'.format(i,j))
Reply
#5
Ok so I did as you suggested and forgot that I did get a slightly different output then I suggested when I moved the 'break' command, 'inside' the loop??  Smile

However the output still doesn't match the example in the book and maybe this is such a small issue I shouldn't focus so much on it, I'm just trying to learn this correctly and don't understand why it's not matching.

When I changed it to this:

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
it gave me this:
Output:
Running i = 1 j = 1 Running i = 1 j = 2 Running i = 1 j = 3
One last thought, the author is using ver (3.5.1) vs. my (3.6.1) could that be enough of a change or even that he could be using a different OS ( I have no idea what OS the author is using) might those explain why I'm seeing something different?

B.t.w. I read the BBcode help and have added the tags as requested, I hope they work, please forgive me if I've blown it again. I can't tell because when I try to preview the post it flashes for about a quarter of a second and then goes back.

Output:
Running i = 1 j = 1 Running i = 1 j = 2 Running i = 1 j = 3 Running i = 2 j = 1 Breaks inner loop at i=2 j=1 Running i = 3 j = 1 Running i = 3 j = 2 Running i = 3 j = 3
Also, note that it is better to us string formatting, i.e. replace

print( 'Running i =', i, 'j =', j )
with

print('Running i = {}, j = {}'.format(i,j))
Your output is an ALMOST an exact match to what I see in my book, however when I tried using your suggested change on my laptop, I'm still getting the same 3 lines as my previous example, so that suggests to me that I must have some sort of issue on my end. I did a brand new clean config/install on an old laptop I'm trying to use to study, perhaps there is a compatibility issue with the OS [Windows 7 home]?? I will try and do some more research.
Thanks again for the assistance!!
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#6
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
In this code snippet if is still outside the second loop and also break is out of the if block, so the break statement is executed always when the execution reaches that line.
So when you start the code and enter the first loop i=1, then you enter the second loop and execute it entirely (i.e. j takes values 1, 2 3) and print the "Running..." string. Then you reach the if statement. When you reach it i=1 and j=3. so the first condition i==2 is evaluated to False AND the second condition j==1 is evaluated to False and thus the whole if statement is evaluated to False (because you have AND i.e. both conditions must be True to evaluate the entire statement as True). Thus "Breaks..." is not printed. Finally you reach the break statement and exit the loop in which it is (i.e. that is the outer loop, the one with i).
Reply
#7
Hey Buran,

Thanks again for the help I appreciate it! Per your suggestion I am going to add the info you mentioned in our PM as a reference in case there is someone out there experiencing the same problem I had.
To address your comment about my output being slightly different here is a comparison

This is the output that you demonstrated and that I got exactly the same when I fixed the indent on the IF block per your instruction.

Output:
Running i = 1 j = 1 Running i = 1 j = 2 Running i = 1 j = 3 Running i = 2 j = 1 Breaks inner loop at i=2 j=1 Running i = 3 j = 1 Running i = 3 j = 2 Running i = 3 j = 3 >>>
This is what the slightly different output example in my book looks like:

Output:
Running i = 1 j = 1 Running i = 1 j = 2 Running i = 1 j = 3 Breaks inner loop at i=2 j=1 Running i = 3 j = 1 Running i = 3 j = 2 Running i = 3 j = 3 >>>
Notice there is no i = 2 j = 1 line here

Also as a quick PS, a later example still in the looping chapter but using a continue statement also gave me a slightly different output as well. I noticed that there was a little sidenote which states: "The continue statement skips the first iteration of the inner loop when the outer loop first runs it" However, this little tidbit contradicts the output I got which after doing some more research on Python 3.6.1 may be a bug. Based on the data from their own documentation ver 3.6.1 is new and very different in a lot of ways to previous versions, and also offered a link to the bug reporting feature.

Here is what I got when I performed the code with the continue statement:

Output:
Running i = 1 j = 1 Continues inner loop at i=1 j=1 Running i = 1 j = 2 Running i = 1 j = 3 Running i = 2 j = 1 Breaks inner loop at i=2 j=1 Running i = 3 j = 1 Running i = 3 j = 2 Running i = 3 j = 3 >>>
Vs. what the example in book using 3.5.1 gives me:

Output:
Continues inner loop at i=1 j=1 Running i = 1 j = 2 Running i = 1 j = 3 Running i = 2 j = 1 Breaks inner loop at i=2 j=1 Running i = 3 j = 1 Running i = 3 j = 2 Running i = 3 j = 3 >>>
I don't know if this is a bug or not and for a newbie like me, seems very trivial, at least in this example but I can imagine that if this were a problem in a production code and was giving an unexpected outcome like this it might be a bigger deal.
I hope this info proves to be helpful to other new coders somewhere if they encounter similar issues.

Thanks again for the help!  Smile
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#8
You should start think in terms of what you want your program to do and how  to achieve it. i.e. you should think of algorithm.
In this particular case - you should ask yourself the question - where you should place the if statement in order to achieve the output from the book (that is without printing anything from second loop).
It comes quite naturally it should be before the print function

for i in range(1,4):
    for j in range(1,4):
        if i==2 and j==1 :
            print('Breaks inner loop at i=2 j=1')
            break
        print('Running i = {}, j = {}'.format(i,j))
Output:
Running i = 1, j = 1 Running i = 1, j = 2 Running i = 1, j = 3 Breaks inner loop at i=2 j=1 Running i = 3, j = 1 Running i = 3, j = 2 Running i = 3, j = 3
Frankly, i find introduction of continue statement in this context but, but anyway if the author thinks like this... I leave to you to try fix the other code in order to achieve the desired result from the book
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No desired output for code. James349 1 981 May-19-2023, 05:43 PM
Last Post: deanhystad
  I need help for adding def to my code muzaffarshamsiev 1 1,677 Mar-28-2021, 02:18 AM
Last Post: BashBedlam
  Wrong output on my code. JTNA 2 7,888 Apr-04-2019, 01:55 PM
Last Post: JTNA
  Code to use output from arduino as input (help) Updownrightleft 0 2,226 Nov-03-2018, 11:04 PM
Last Post: Updownrightleft
  What this code will output and why? Valgard 4 3,872 Aug-30-2017, 01:41 PM
Last Post: Valgard

Forum Jump:

User Panel Messages

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