ps2a help. hey, have't been keepin up my programming for the last month (Had family stuff on), so now im back and revised the last program for mcnuggets i tried to write, and changed how i went about it, my problem is this, i think my code is ok, but when i execute it, it doesn't seem to register that when n=6, nuginrow should +1 and n should +1, it gives me 6 as the value of n, but treats it as though 6 is not equal to 6a + 9b + 20c an carries on with my else: statement, heres my code
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
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.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
The issue with your code can be trace to how you exit your 3 nested 'for' loops. You actually don't exit them until all ranges of a, b, and c have been looped through. Try doing it on paper, like the following:
iteration,nonug,nuginrow,n,a,b,c : result
6*a+9*b+20*c ==n ==> 0+0+0 == 1 ==> false
update nonug, n, nuginrow, and c (note, you're still inside the for loop for c )
6*a+9*b+20*c == n ==> 0+0+20 == 2 ==> false
update nonug, n, nuginrow, and c (note, you're still inside the for loop for c)
As you can see you're stuck inside the for loop for c for 100 times before you even move to b=1, that's why you're seeing you script constantly going to your else clause.
You need to now figure out the correct loop termination for each of the for loops in your script. If you need more help, post back. I'll answer when I can.
loop termination is the key .. how to find it
lets take 50 nuggets for example
1 loop for 6 == 50/6 the loop termination is <= 8
inner loop for 9 == 50/9 the loop termination is <=5
innermost loop for 20 == 50/20 the loop termination is <=2
oh, ok, i guess im confused, not used true or false statements yet (not sure if it was in the video,) ill go back and have a look, thanks
Not the answer you are looking for? Search for more explanations.
the true or false statement in my comment was not meant to be code. I'm just indicating that the comparison results would be false.
It looks like the number you are trying to test is 'n' and that you are incrementing it on every loop of the 'c' variable (you are changing the number you are testing INSIDE of the c loop). I'm no expert but it does not seem wise to change the thing you are testing inside of the inner loop.
Probably what you want to do is pick a number (n) then loop through the a,b, and c coefficients to see if it can be bought. Only after you have determined that n can or cannot be bought by various combinations of a, b, c should you increment n. In other words you should only increment n OUTSIDE of you three (a,b,c) for loops.
As stated above you also need to figure out a way to stop each of your for loops (a,b,c) from looping. A break statement will 'break out of' a single for loop - if you use a break statement you need one for each loop and you need to figure out when to break out of it. There may be other ways to stop your loops.
I went at it a bit differently. I used the same double nested for loops, but I didn't bother checking for 6 in a row. I just started at 200 and worked backwards, returning the first number that didn't add up. This script has a bunch of jewellery and I put in some checks in to ensure the user doesn't enter something that's not an integer, or is less than two, or is a number they've already entered.
Has anyone got any idea for how to write a unit test to check these scripts? I'll swap you a medal for a test script.
I might email MIT and ask them if they still have the test scripts and if they'll release them.
hmmm, that makes sense... bet you got away with one less variable or looop. just goes to show if you think about a problem first you might find a better way to solve it.
Were you thinking about using the actual unittest module in the Python library?? Later in ps10 i think they included an xtra script that they called a test harness. you have to have know the answer for specific function arguments then compare the function's result against that known answer.
That's the bit I'm stuck on ~ how you'd generate a list of correct answers for these scripts without relying on the scripts themselves.
loop = True
loop = False
stuff. I keep doing that out of habbit, I do use it sometimes, but I should've done
in this case.
My posts are getting really bad for typos. Sorry.