A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • one year ago

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

  • This Question is Closed
  1. Lyrae
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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.

  2. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  3. Lyrae
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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
  4. rsmith6559
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  5. mathmate
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

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

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.