anonymous
  • anonymous
pset 2 part 1, mcnuggets! im wondering if im a mile of with my code, im trying to have the user input how many mcnuggets they want, then the program tell them if it's possible, and how many of each nugget pack they have to buy, where a is 6 pack b 9 and c 20, have a look at next post for 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 :)
SOLVED
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.
chestercat
  • chestercat
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
n = raw_input('How Many Nuggets Do You Want?: ') for a in range (0, 10): for b in range (0, 10): for c in range (0, 10): if n==6*a+9*b+20*c: print 'a',a,'b',b,'c',c if n==6*a+20*c+9*b: print 'a',a,'b',b,'c',c if n==9*b+6*a+20*c: print 'a',a,'b',b,'c',c if n==9*b+20*c+6*a: print 'a',a,'b',b,'c',c if n==20*c+9*b+6*a: print 'a',a,'b',b,'c',c if n==20*c+6*a+9*b: print 'a',a,'b',b,'c',c else: print 'no nuggets!' please also bear in mind that im trying to only use skills collected up to and including lecture 3
anonymous
  • anonymous
I just worked on this program as well, and I'm pretty new. I think we have mostly the same stuff, but I don't know why you have so many if statements. The nested For loops should take care of all of the different options of a, b, c. Also, the high end of your range is limited too small in my opinion. I would recommend using your n variable. If you wanted to minimize the number of calcs you could refine the top end of your range further by taking advantage of pack sizes as well as n. This way you minimize the number of calcs it has to go through, and is dynamic enough to work with the user input.
anonymous
  • anonymous
hey, i got all the if statements jus because when i origionally did it, it didn't work, so i just tried putting the combos in myself, so asides from that, can you see anything wrong? how long should it take to calculate? cheers

Looking for something else?

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

More answers

anonymous
  • anonymous
Ok, I did what you did and first I think you have to turn the raw input into an integer using int(). When you look at your if statements, they all say the same thing, so just have one. and have a variable that is False if there are no solutions, and True if there are solutions. an example of what I mean is this: solutionFound = False for x in range(1,10): if x==5: solutionFound = True if not solutionFound: print "The range does not include the integer 5" And if you want to decrease the number of calculations, put a limit on the upper portion of the range by making it a function of n. Remember, that each for loop the upper limit on n should be able to decrease if the previuous for loop has a value for n. I.e. if a=1, then the max b could be is (n-a*6)/9 +1 and so on and so forth. Hope this helps
anonymous
  • anonymous
I believe this code is easier to do if you set up a system of equations. Yours may or may not work the way it its, I didn't go through and try to debug it I know you said that you are trying to use only the knowledge acquired in the first 3 lectures but in the fourth lecture the professor does a problem very similar to this.
anonymous
  • anonymous
^O my bad I wish you could delete posts I accidentally skipped over zakanoth's corrections
anonymous
  • anonymous
I think this is much easier than you are trying to make it. Does your programming language have integer division and modulus functions? For example in certain languages 10 div 3 = 3 because it finds the answer and then drops all remainders. Then 10 mod 3 = 1 because the mod function is the remainder only. So... in your program you are looking for a number that is divisible by 6,9, or 20. The way you decide if a number is divisible by a number is to "mod" it and if the answer = 0 then it is divisible by that number.. So if you took you n nuggets and mod /6 if answer = 0 then n is divisible by 6 if you took n mod 9 and the answer = 0 then n is divisible by 9 same with 20. Then to find out how many packs you need to find how many times the 6,9, or 20 goes into your n.. which is n div 6 or n div 9 or n div 20.....
anonymous
  • anonymous
wouldn't the way your explaining it only work if the number of nuggets is a multiple of 6,9,20
anonymous
  • anonymous
hi. i'm not sure why you have to have all the different permutations of a, b, and c. I don't think that they have to be there to have all the possible combinations because the for loop does that already for one if n== 6*a + 9*b + 20*c
anonymous
  • anonymous
Look at zakanoth's corrections he already fixed his code.
anonymous
  • anonymous
wow, thanks for all the input, to all those who are saying i don't need all the ifs, i tried replying the other day and didn't know if it worked, i only put them as my origional code didn't work, (didn't change it back before i posted it.) Just to eliminate that this was not the problem, again thanks for the help, i think iv found my problem, i swapped raw_input for just input, zackanoth gave me the idea (thanks!!!) that the input wasn't being sein as an integar, i only have one problem now, it gives me the answer, but then after also gives me the reply for non possible combinations, here's my revised code... n = input('How Many Nuggets Do You Want?: ') for a in range (0, 10): for b in range (0, 10): for c in range (0, 10): if n==6*a+9*b+20*c: print 'a',a,'b',b,'c',c else: print 'no nuggets!' please also note this is to answer if possible combinations are divisable, not the same as part 3 where im trying to find 6 consecutive numbers that aren't divisable. Thanks once again for all the support! You guys are keeping me going lol!
anonymous
  • anonymous
also note that my code gives all possible combos of boxes, not just one (could fix this with a statement instead of printing boxes), just need to sort out the else statement now lol
anonymous
  • anonymous
You may get multiple answers but they shouldn't be wrong. I would say you should have two functions . 1 that contains the code you already have and 1 that calls upon that code, that way you could use a return function and it would only give you 1 answer. if you do it this way you wouldn't need the else just put (print "No Nuggets!") no parens tho
anonymous
  • anonymous
im not sure i follow you? i mean i get what your saying, its because my else isn't in relation to my if is it? as its at the beggining of a new line of code, and there is no if above it at the top, but it i put the else under my if, i just get my else statement, and not my if? lol
anonymous
  • anonymous
I'm not completely sure if the way I'm explaining is taught before lecture 3 but if you have done the readings or outside research you may know. I recommend using a self defined function using the def function command. The script that you already have would be the body of one function and then you would need to make another that calls upon that function. I redid it for you but you should try to do it yourself first unless you don't know how to use the def function. http://dpaste.com/hold/528488/ .
anonymous
  • anonymous
I'm not completely sure if the way I'm explaining is taught before lecture 3 but if you have done the readings or outside research you may know. I recommend using a self defined function using the def function command. The script that you already have would be the body of one function and then you would need to make another that calls upon that function. I redid it for you but you should try to do it yourself first unless you don't know how to use the def function. http://dpaste.com/hold/528488/ .
anonymous
  • anonymous
Hey, i managed to do it, well, i just removed the else, so the program does not do anything if theres no way to get the combo desired, but as the question is just asking to show how to make 50-64 nuggets or whatever, and said i can just write it down, i think it will be ok, i also redid the program after this, so the user does not have to input their own number, it just calculates 50, then 51, etc, then at the end says 'press enter for answer in writing to question 2', i cheated and found out on the internet how to get the program to pause, which has helped my put my answers more neatly, ask the user to continue etc.
anonymous
  • anonymous
One thing i do want to ask the python pros thou, i want to create a while statement. and in that while statement a for loop, and inside that loop, i want to create an if and an else statement, and then i want to create another else statement, (incase the while statement is not met). I thought i nailed it, but get either errors or an answer of 1000001, here it is if you could take a look: http://dpaste.com/528609/ p.s to onyx, yes mate havent done much python before these and don't think thats been covered in the videos yet to where im at, youve helped me loads though.
anonymous
  • anonymous
Yea sorry I already knew about that because when I'm not home and don't have Internet I read the text book, Think Like a Computer Scientist. So I couldn't remember whether I was supposed to already know or not. And it's not cheating to look around and find ways to improve your code sometimes it is necessary to do outside research.
anonymous
  • anonymous
And I'm working on your code right now. Give me a sec I almost have it working.
anonymous
  • anonymous
Okay you had the right idea but you were kind of just putting stuff in the wrong places. I will post your code at the end. I am going to try to explain what I did. You should try doing it on your own before looking at my code. Okay so first you needed a way to keep track of whether a solution was found or not. So you could put something like solution=false at the beginning and if it goes through your if statement if a*6+b*9+c*20==n you would put soution=true. This way at the end you could use an if statement about whether it was true(meaning it found a solution) or false. Now this is where I said you had the right idea you just put stuff in the wrong spots. anything that you thought would go under the if statement if a*6+b*9+c*20==n would now go under your new if statement if solution==true except the print statements because those will just slow down your code. And under the else statement to the if statement if solution==true you need to reset your counting statement to 0 because no solution was found and add 1 to your number of nuggets. Okay here is your finished code I have one version where I left what you did wrong in just commented out so you can see changes and then the second one will be the code itself. http://dpaste.com/hold/528671/ and here is the one without commented changes.
anonymous
  • anonymous
Omg I was on the last line of typing that huge response and my computer crashed I was about to rage but when I turned it back on it was still on the screen.
anonymous
  • anonymous
and here is the one without changes commented out http://dpaste.com/hold/528684/
anonymous
  • anonymous
# This program will let you find the values combinations of a diphontine equation # required libraries import math for Requested_nuggets in range(50) : #Main Is_exit = 0 #Requested_nuggets = int(raw_input("Enter the No of nuggets you want?")) #assert type(Requested_nuggets) is int, "Entered No is not an integer: %s" % `Requested_nuggets` quantity_constants = [ 6, 9, 20 ] quantity_constants.sort #going to solve the equation using exhaustive method #find the max value of the multiple of each constant flag, Sum = 0,0 a = Requested_nuggets/quantity_constants[0] b = Requested_nuggets/quantity_constants[1] c = Requested_nuggets/quantity_constants[2] x = 0 while x <= a : y = 0 while y <= b: z = 0 while z <= c: Sum = (x*quantity_constants[0]) + (y*quantity_constants[1]) + (z*quantity_constants[2]) #print `x, y, z` if Sum == Requested_nuggets : print `Sum` flag = 1 break z = z + 1 if flag == 1 : break y = y + 1 if flag == 1 : break x = x + 1 if flag == 0 : print "The requested number of nuggets cannot be obtained"
anonymous
  • anonymous
# This program will let you find the values combinations of a diphontine equation # required libraries import math for Requested_nuggets in range(50) : #Main Is_exit = 0 #Requested_nuggets = int(raw_input("Enter the No of nuggets you want?")) #assert type(Requested_nuggets) is int, "Entered No is not an integer: %s" % `Requested_nuggets` quantity_constants = [ 6, 9, 20 ] quantity_constants.sort #going to solve the equation using exhaustive method #find the max value of the multiple of each constant flag, Sum = 0,0 a = Requested_nuggets/quantity_constants[0] b = Requested_nuggets/quantity_constants[1] c = Requested_nuggets/quantity_constants[2] x = 0 while x <= a : y = 0 while y <= b: z = 0 while z <= c: Sum = (x*quantity_constants[0]) + (y*quantity_constants[1]) + (z*quantity_constants[2]) #print `x, y, z` if Sum == Requested_nuggets : print `Sum` flag = 1 break z = z + 1 if flag == 1 : break y = y + 1 if flag == 1 : break x = x + 1 if flag == 0 : print "The requested number of nuggets cannot be obtained"

Looking for something else?

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