A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

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 http://pastebin.com/6nfMPUyM

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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 iteration=1,nonug=0,nuginrow=0,n=1,a=0,b=0,c=0 : 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 ) iteration=2,nonug=1,nuginrow=0,n=2,a=0,b=0,c=1: 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.

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

    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

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  6. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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. http://pastebin.com/EgF3m3xi 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.

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  8. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  9. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Ignore loop = True while loop: if something: loop = False stuff. I keep doing that out of habbit, I do use it sometimes, but I should've done while True: if something: break in this case.

  10. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    *habit My posts are getting really bad for typos. Sorry.

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