Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 20 Online
OpenStudy (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

OpenStudy (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.

OpenStudy (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

OpenStudy (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 ``` ??

OpenStudy (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.

OpenStudy (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".

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!