anonymous
  • anonymous
Conditional working half the time: while (n < 50): if (n > 0): if (n > 6): print 'n superior to 6' n = n + 1 else: n = n + 1 print 'n inferior to 6' else: n = n + 1 n inferior to 6 n inferior to 6 n inferior to 6 n inferior to 6 n inferior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6
MIT 6.00 Intro Computer Science (OCW)
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this
and thousands of other questions

Lyrae
  • Lyrae
Hello and welcome to OS! Nope. This works just fine. It will for 6 iterations print ``` n inferior to 6 ``` and then print ``` n superior to 6 n inferior to 6 ``` This happens because your second print is outside the nestled if-else expression, so it always prints "n inferior to 6" if n>0, regardless if n>6. In close to 100% of the cases a program is not working it because of flawed logic or broken code. When I run into similar issues I try to trace the program with a debugger or with plain pen and paper. It is really good exercise an you get to practice programmatic thinking.
anonymous
  • anonymous
I've thought about that too, when I removed the (n > 0) condition, it still returns the same results. while (n < 50): if (n > 6): n = n + 1 print 'n superior to 6' else: n = n + 1 print 'n inferior to 6' n inferior to 6 n inferior to 6 n inferior to 6 n inferior to 6 n inferior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 Oddly enough, if i inverse the conditions, the results become dodgy inside the (n < 6), maybe the error is there but how so ? while (n < 50): if (n < 6): n = n + 1 print 'n inferior to 6' else: n = n + 1 print 'n superior to 6' n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n inferior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6 n superior to 6
Lyrae
  • Lyrae
Let's analyse the code a bit. without "if n>0": ``` while (n < 50): if (n > 6): # Increment n and print 'n superior to 6' if n>6 n = n + 1 print 'n superior to 6' else: n = n + 1 # Otherwise increment n only print 'n inferior to 6' # Print 'n inferior to 6' if n<50 ``` Inverse condition: ``` while (n < 50): if (n < 6): # Increment n and print 'n inferior to 6' if n<6 n = n + 1 print 'n inferior to 6' else: n = n + 1 # Otherwise increment n only print 'n superior to 6' # Print 'n superior to 6' if n<50 ``` Graphical representation is also good (see attachment) so I made a small flowchart of the first code in this post. Do you notice why you get ``` n superior to 6 n inferior to 6 ``` ??
1 Attachment

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

rsmith6559
  • rsmith6559
Aside from commenting your code to help you see what's going on, why don't you rewrite it so that "n = n + 1" only appears once? That will probably show you the logic issue with the last print statement.
mathmate
  • mathmate
Original code: ``` while (n < 50): if (n > 0): if (n > 6): print 'n superior to 6' n = n + 1 else: n = n + 1 print 'n inferior to 6' else: n = n + 1 ``` In summary, @Lyrae has pointed out that there is improper nesting of the print statements @rsmith6559 suggested putting the increment of n (n=n+1) prior to the end of the while loop, this will avoid incrementing n BEFORE printing "n inferior to 6", which is incorrect. I suggest to, in addition, 1. print the value of n instead of the character n, such as ``` print(n," is superior to 6") ``` to help you debug the logic, and 2. Add the case of equality, i.e. when n==6. This can be done by a. skipping the printing when n==6 b. printing "n is equal to 6".

Looking for something else?

Not the answer you are looking for? Search for more explanations.