A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
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
anonymous
 5 years ago
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

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0n = 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
 5 years ago
Best ResponseYou've already chosen the best response.0I 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
 5 years ago
Best ResponseYou've already chosen the best response.0hey, 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Ok, 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 (na*6)/9 +1 and so on and so forth. Hope this helps

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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
 5 years ago
Best ResponseYou've already chosen the best response.0^O my bad I wish you could delete posts I accidentally skipped over zakanoth's corrections

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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
 5 years ago
Best ResponseYou've already chosen the best response.0wouldn't the way your explaining it only work if the number of nuggets is a multiple of 6,9,20

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0hi. 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
 5 years ago
Best ResponseYou've already chosen the best response.0Look at zakanoth's corrections he already fixed his code.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0wow, 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
 5 years ago
Best ResponseYou've already chosen the best response.0also 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
 5 years ago
Best ResponseYou've already chosen the best response.0You 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
 5 years ago
Best ResponseYou've already chosen the best response.0im 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
 5 years ago
Best ResponseYou've already chosen the best response.0I'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
 5 years ago
Best ResponseYou've already chosen the best response.0I'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
 5 years ago
Best ResponseYou've already chosen the best response.0Hey, 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 5064 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
 5 years ago
Best ResponseYou've already chosen the best response.0One 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
 5 years ago
Best ResponseYou've already chosen the best response.0Yea 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
 5 years ago
Best ResponseYou've already chosen the best response.0And I'm working on your code right now. Give me a sec I almost have it working.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Okay 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
 5 years ago
Best ResponseYou've already chosen the best response.0Omg 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
 5 years ago
Best ResponseYou've already chosen the best response.0and here is the one without changes commented out http://dpaste.com/hold/528684/

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0# 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
 5 years ago
Best ResponseYou've already chosen the best response.0# 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"
Ask your own question
Sign UpFind more explanations on OpenStudy
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
 Engagement 19 Mad Hatter
 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.