Struggling with PS2, P3. I can't debug the following program. Can anyone help?
n=1
a=0
b=0
c=0
while n<50: # when n=6,
for c in range (0,3): # and c=0,
remainder = n-(20*c) # remainder=6
for b in range (0,6): # and b=0
a = remainder-(9*b) # a=6
a = a/6 # a=1
if 6*a + 9*b + 20*c == n:
# 6(1) + 9(0) + 20(0) = 6, right?
print n," can be bought"
n += 1
print n," cannot be bought"
n += 1

- anonymous

- Stacey Warren - Expert brainly.com

Hey! We 've verified this expert answer for you, click below to unlock the details :)

- katieb

I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!

- anonymous

I tried to put my question in the comments but realized it could be confusing. My comments are basically walking through the script for n=6, c=0, b=0. I feel as if the equation should equal n (6). Why does it print "6 cannot be bought"?

- anonymous

What's most confusing is the variables having obfuscated names. The lines which have comments which don't match up with the logic are (with corrected comments):
05 while n<50: # when n=anything under 50 -- right now equaling 1
07 remainder = n-(20*c) # remainder=1
and from here everything hinders on this bug
I'm guessing you wanted line 01 to be n=6?

- anonymous

that's right. i admittedly asked the question very poorly and apologize.
i initiate at n=1, but increment at n=n+1, so eventually, n will equal 6 as it cycles through the loop. when n does reach 6, i would expect line 11 to be true and therefore print "6 can be bought" however as the program is written now it prints "6 cannot be bought" which is where i'm stuck.
does that clarify my question or am i still missing something?

Looking for something else?

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

## More answers

- anonymous

why dont you put n=6 as initial condition? does it answer your question?

- anonymous

it doesn't answer my question, but it certainly helps. if start with n=6, it does in fact print "6 can be bought" which is what i would expect. however, if i start with n=9 it prints "9 cannot be bought" so there seems to be a problem with (1) how the code block determines the validity of 6a+9b+20c=n, and (2) how it cycles through the potential combinations of a, b, and c (since when i start with n=1, 6 fails). will keep working on it. thanks jeffanthony and jans!

- anonymous

I think you problem as to do with "n"... I am not a programmer of this language but here goes... As you loop "b" around, you are changing the value of "n" every time. Then it will go back to the c loop and loop again with "n" continuing to increase 6 more times with the loop b.. and then again continuing to increase with the next loop of c.. By the time you get to the end of the while statement... "n" is huge. It will never be 6 when it gets to the top of the while loop.

- anonymous

If I am just way off.... forgive me... :-)

- anonymous

OK I see what's going on here now:
Your if statement should have an else for the 'cannot be bought' status --
if (6*a)+(9*b)+(20*c) == n:
print n,' can be bought'
else:
print n, 'cannot be bought'
n+=1

- anonymous

I agree with jeffanthony but don't you think there is a problem with n increasing within the two loops so when it loops around to the while loop, the loop will not try every number from 1 - 49...

- anonymous

i broke the problem down further to try to identify one of the underlying issues. instead of cycling through different "n"s, i created a program that should test one specific value n. i am able to get the right answer but cannot figure out how to avoid printing "n cannot be bought." I've tried adjusting the indentation of the print statement but that doesn't help. Thoughts?
a=0
b=0
c=0
n=raw_input ("Enter a number of McNuggets to test: ")
n=int(n)
for c in range (0,n):
remainder = n-(20*c)
for b in range (0,n):
a = remainder-(9*b)
a = a/6
if 6*a + 9*b + 20*c == n:
if a>=0 and b>=0 and c>=0:
print n," can be bought",a,b,c
print n," cannot be bought"

- anonymous

when i enter n=1, i get:
1 cannot be bought
when i enter n=6, i get:
6 can be bought 1 0 0
6 cannot be bought
when i enter n=15, i get:
15 can be bought 1 1 0
15 cannot be bought

- anonymous

you're missing the else: clause still

- anonymous

Use this code, in fact there is no way you'll get a=1,b=0,c=0:
n=1
a=0
b=0
c=0
while n<50: # when n=6,
for c in range (0,3): # and c=0,
remainder = n-(20*c) # remainder=6
for b in range (0,6): # and b=0
a = remainder-(9*b) # a=6
a = a/6 # a=1
result = 6*a + 9*b + 20*c
if (result) == n:
# 6(1) + 9(0) + 20(0) = 6, right?
print (a,b,c,result,n," can be bought")
else:
print (a,b,c,result,n," cannot be bought")
# n += 1
n += 1

- anonymous

but if you want to push to get result 6 can be bought, USE n = 0 as initial condition.

- anonymous

n=0
a=0
b=0
c=0
while n<50: # when n=6,
for c in range (0,3): # and c=0,
remainder = n-(20*c) # remainder=6
for b in range (0,6): # and b=0
a = remainder-(9*b) # a=6
a = a/6 # a=1
result = 6*a + 9*b + 20*c
if (result) == n:
# 6(1) + 9(0) + 20(0) = 6, right?
print (a,b,c,result,n," can be bought")
else:
print (a,b,c,result,n," cannot be bought")
# n += 1
n += 1

- anonymous

And also if you want user to enter input from keyboard, i modify like this:
hope you can see the difference because i made a into integer value..
#n=0
a=0
b=0
c=0
n=raw_input ("Enter a number of McNuggets to test: ")
n=int(n)
while n<50: # when n=6,
for c in range (0,3): # and c=0,
remainder = n-(20*c) # remainder=6
for b in range (0,6): # and b=0
a = remainder-(9*b) # a=6
a = int(a/6) # a=1
result = 6*a + 9*b + 20*c
if (result) == n:
# 6(1) + 9(0) + 20(0) = 6, right?
print (a,b,c,result,n," can be bought")
else:
print (a,b,c,result,n," cannot be bought")
# n += 1
n += 1

- anonymous

DT I see your still viewing this post and I saw you viewing my explanation to jaymorgan of this problem do you still need help or were their explanations enough?

- anonymous

well, i was able to build a program that determines whether any number, n, can be bought. so that's a good start. but i've been having trouble getting that code to loop correctly. here is the program that works:
n=raw_input ("Enter a number of McNuggets to test: ")
n=int(n)
can_buy=False
for c in range (0,n):
for b in range (0,n):
for a in range (0,n):
if 6*a + 9*b + 20*c == n:
can_buy=True
print n," nuggets can be bought",a,b,c
if can_buy==False:
print n," nuggets cannot be bought"

- anonymous

Here is my attempt of implanting that code into problem 3 (adding a while and a for loop)...
counter = 0
capture = ()
can_buy=False
while counter < 6:
for n in range(1,50):
for a in range (0,n):
for b in range (0,n):
for c in range (0,n):
if 6*a + 9*b + 20*c == n:
can_buy=True
print n," can be bought",a,b,c
if can_buy==False:
print n," cannot be bought"
capture += (n,) # add n to numbers that can't be bought
counter=0 # reset counter to zero as streak broken
n+=1
counter+=1 # track number of cons ints that can't be bought
n+=1
print "Largest number of McNuggets that cannot be bought in exact quantity: ",capture[-1]

- anonymous

what seems to be happening is that once a number is found that can be bought (in this case the first is 6), it stops going through the second if statement and ignores all values of n that do not have a solution to the equation. not sure how to fix this :(

- anonymous

Ok give me a second to see whats wrong.

- anonymous

can you post your code on dpaste.com for me please because when you just put it in here its annoying to copy to a python file

- anonymous

sure here it is...
http://dpaste.com/528841/

- anonymous

and thanks for taking a look over it for me!

- anonymous

Yea no problem, I don't understand why you have your capture tuple? Why do you need the tuple instead of just printing the output.

- anonymous

nevermind I get it

- anonymous

Okay I got it your code was actually really good. I wouldn't have thought to do it this way haha. You only had a few things wrong. Your assignment statement for can_buy==false has to be inside of your while and for loop because the way you have it now it never gets reset. It is false in the beginning but once it gets one true there is no reset so it thinks everything else is true. I changed your while counter<6 to while counter<5. And I added the elif statement after your if statement if can_buy==False. The elif statement is there to add one to the counter only if solution is found. The way you had it it would add one to the counter no matter what. Here is your code I deleted your comments and only put comments on your corrections http://dpaste.com/hold/528845/.

- anonymous

And don't just be satisfied with having the answer if you don't understand why I did something just ask and I'll explain.

- anonymous

- anonymous

onyx, you are a lifesaver. i was really struggling with this! i do have a few questions though.
(1) what was the rationale for changing the count from <6 to <5?
(2) did you limit the a/b/c to range 0,15 (vs 0,n) to save computations or is there an additional reason?
(3) i would never have realized that the can_buy=false needed to be inside the loop! that was huge. i didn't get it at first but now understand why.
(4) i think i get why you added the elif. but i'm going to have to go over that and may have an additional question.
really appreciate your help. i'm gonna call it a night. thanks again onyx!

- anonymous

1. Kind of confusing to explain but basically your first count isn't 1 its 0 because it is not incremented until the if and elif statements. So the first time through your code when naturally you would expect it to be one it is really zero.
2.Yes I did this to save on computations because I knew the necessary numbers weren't above like 9 or 10 but I put 15 to leave a little cushion but until you know what the answer is you shouldn't do this. That is my fault I forgot to change it back.
4.The elif is no different from an if other than it will only execute if the if statement doesnt work. So really since there is no way for both to be true you can you can just use an if that makes more sense to you.
And No Problem

- anonymous

second to last line is supposed to say "you can us an if, if that makes more sense to you"

- anonymous

i went over the program again. the way it is working now, the main while loop doesn't stop after the counter gets to 5. if i change the while loop to "while counter<2" or any other number, the program runs the exact same way. it only stops because of the range in the for loop (1,50). do you have any idea why the program doesn't terminate once the counter reaches the limit we put on it via the while statement?

- anonymous

You don't even need the while statement sorry I didn't realize it wasn't doing anything. The reason the code works with or without it is because you put your non solutions into a list. No matter what your last number in your list is going to be your last possible solution. If you want it to terminate after it reaches the last possible solution you would need to delete the for loop.

- anonymous

If you delete your for loop you have to re add your increments of n into the else statement and elif statement. Also because you are using the list your while<# should be 6 but notice it doesn't terminate at 43 with yours program if you put a print statement at the bottom for n it will print 50 because with <6 again the counter doesn't raise until the end of the code so when at 42 counter was 5 and then it got reset to 0 on 43. So then it will run through again until it reaches 6 and it will increment to 50 when the counter gets to 6. Sorry a little confusing explanation. It is possible to make your program terminate correctly but it would take away your idea of using a list and I liked the list because its your style I haven't seen anyone else try it like that.

Looking for something else?

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