Re: [Tutor] Problem
Thankyou so much for taking the time it really means alot. I'll change the code and try again. I have just one question what purpose does " {} " serve here? Sent from my iPhone > On 29-Aug-2016, at 2:30 AM, zakaria wrote: > > if you print the values of a, b ,c that satisfy and don't satisfy the > condiction cm == 50 at the same time, you can't know what works and > what did not work. > > here is the code that i wrote and worked well > > for a in range(1, 11): # i replaced 10 by 11 to include the 10 > for b in range(1, 6): # same as before put 6 to include 5 > for c in range(1, 6): > mc = (6*a) + (9*b) + (20*a) > if mc == 50: > print(a, b, c) > > notice that i am using python 3.5, range is a generator like xrange in > python 2 > > and the result is 2 for a and b and 1 for c > I wish this coold help you > > for what you said, here is the hard coded solution: > > for a in range(1, 11): >for b in range(1, 6): > for c in range(1, 6): >mc = 6*a + 9*b + 20*c >if mc == 50: > print('for 50 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, > c)) >if mc == 51: > print('for 51 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, > c)) >if mc == 52: > print('for 52 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, > c)) >if mc == 53: > print('for 53 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, > c)) >if mc == 54: > print('for 54 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, > c)) >if mc == 55: > print('for 55 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, > c)) > and the result was > > for 55 McNuggets, "a"= 1, "b"=1, "c"=2 > for 53 McNuggets, "a"= 1, "b"=3, "c"=1 > for 50 McNuggets, "a"= 2, "b"=2, "c"=1 > for 53 McNuggets, "a"= 4, "b"=1, "c"=1 > > > Or you can write a more elegant one: > > for a in range(1, 11): >for b in range(1, 6): > for c in range(1, 6): >mc = 6*a + 9*b + 20*c >if mc in range(50, 56): > print('for {} McNuggets, "a"= {}, "b"={}, "c"={}'.format(mc, > a, b, c)) > > i used range(50, 56) to include the value 56 > > know , i wanted to know the purpose of this code: >> a=+1 >> b=b+1 >> c=c+1 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem
I changed the code a bit and this is the result: for a in range(1,11): for b in range(1,6): for c in range(1,6): mc=(6*a)+(9*b)+(20*c) if mc==50: print 'For 50 McNuggets:''a=',a,'b=',b,'c=',c if mc==51: print 'Fpr 51 McNuggets:''a=',a,'b=',b,'c=',c if mc==52: print 'For 52 McNuggets:''a=',a,'b=',b,'c=',c if mc==53: print 'For 53 McNuggets:''a=',a,'b=',b,'c=',c if mc==54: print 'For 54 McNuggets:''a=',a,'b=',b,'c=',c if mc==55: print 'For 55 McNuggets:''a=',a,'b=',b,'c=',c Result: For 55 McNuggets:a= 1 b= 1 c= 2 For 53 McNuggets:a= 1 b= 3 c= 1 For 50 McNuggets:a= 2 b= 2 c= 1 For 53 McNuggets:a= 4 b= 1 c= 1 Two questions: 1) why is it printing backwards? 2) why is it skipping 51,52 and 54? On Mon, Aug 29, 2016 at 3:21 AM, Shahan Khan wrote: > Thankyou so much for taking the time it really means alot. I'll change the > code and try again. I have just one question what purpose does " {} " serve > here? > > Sent from my iPhone > > > On 29-Aug-2016, at 2:30 AM, zakaria wrote: > > > > if you print the values of a, b ,c that satisfy and don't satisfy the > > condiction cm == 50 at the same time, you can't know what works and > > what did not work. > > > > here is the code that i wrote and worked well > > > > for a in range(1, 11): # i replaced 10 by 11 to include the 10 > > for b in range(1, 6): # same as before put 6 to include 5 > > for c in range(1, 6): > > mc = (6*a) + (9*b) + (20*a) > > if mc == 50: > > print(a, b, c) > > > > notice that i am using python 3.5, range is a generator like xrange in > > python 2 > > > > and the result is 2 for a and b and 1 for c > > I wish this coold help you > > > > for what you said, here is the hard coded solution: > > > > for a in range(1, 11): > >for b in range(1, 6): > > for c in range(1, 6): > >mc = 6*a + 9*b + 20*c > >if mc == 50: > > print('for 50 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, > > c)) > >if mc == 51: > > print('for 51 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, > > c)) > >if mc == 52: > > print('for 52 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, > > c)) > >if mc == 53: > > print('for 53 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, > > c)) > >if mc == 54: > > print('for 54 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, > > c)) > >if mc == 55: > > print('for 55 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, > > c)) > > and the result was > > > > for 55 McNuggets, "a"= 1, "b"=1, "c"=2 > > for 53 McNuggets, "a"= 1, "b"=3, "c"=1 > > for 50 McNuggets, "a"= 2, "b"=2, "c"=1 > > for 53 McNuggets, "a"= 4, "b"=1, "c"=1 > > > > > > Or you can write a more elegant one: > > > > for a in range(1, 11): > >for b in range(1, 6): > > for c in range(1, 6): > >mc = 6*a + 9*b + 20*c > >if mc in range(50, 56): > > print('for {} McNuggets, "a"= {}, "b"={}, "c"={}'.format(mc, > > a, b, c)) > > > > i used range(50, 56) to include the value 56 > > > > know , i wanted to know the purpose of this code: > >> a=+1 > >> b=b+1 > >> c=c+1 > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem
Yes i format my code but i can't figure out this new problem On Mon, Aug 29, 2016 at 3:20 AM, Joel Goldstick wrote: > On Sun, Aug 28, 2016 at 10:46 AM, shahan khan > wrote: > > Hello > > I'm teching myself Python using MIT opencourse ware. I'm a beginner and > > have some what knowledge of c and c++. I'm using Python version > > > > Here is the problem: > > McDiophantine: Selling McNuggets > > In mathematics, a Diophantine equation (named for Diophantus of > Alexandria, > > a third century Greek mathematician) is a polynomial equation where the > > variables can only take on integer values. Although you may not realize > it, > > you have seen Diophantine equations before: one of the most famous > > Diophantine equations is: > > xn + yn= zn. > > For n=2, there are infinitely many solutions (values for x, y and z) > called > > the Pythagorean triples, e.g. 32 + 42 = 52. For larger values of n, > > Fermat’s famous “last theorem” states that there do not exist any > positive > > integer solutions for x, y and z that satisfy this equation. For > centuries, > > mathematicians have studied different Diophantine equations; besides > > Fermat’s last theorem, some famous ones include Pell’s equation, and the > > Erdos-Strauss conjecture. For more information on this intriguing branch > of > > mathematics, you may find the Wikipedia article of interest. > > We are not certain that McDonald’s knows about Diophantine equations > > (actually we doubt that they do), but they use them! McDonald’s sells > > Chicken McNuggets in packages of 6, 9 or 20 McNuggets. Thus, it is > > possible, for example, to buy exactly 15 McNuggets (with one package of 6 > > and a second package of 9), but it is not possible to buy exactly 16 > > nuggets, since no non-negative integer combination of 6’s, 9’s and 20’s > > adds up to 16. To determine if it is possible to buy exactly n McNuggets, > > one has to solve a Diophantine equation: find non-negative integer values > > of a, b, and c, such that > > 6a + 9b + 20c = n. > > Problem 1. > > Show that it is possible to buy exactly 50, 51, 52, 53, 54, and 55 > > McNuggets, by finding solutions to the Diophantine equation. You can > solve > > this in your head, using paper and pencil, or writing a program. However > > you chose to solve this problem, list the combinations of 6, 9 and 20 > packs > > of McNuggets you need to buy in order to get each of the exact amounts. > > Given that it is possible to buy sets of 50, 51, 52, 53, 54 or 55 > McNuggets > > by combinations of 6, 9 and 20 packs, show that it is possible to buy 56, > > 57,…, 65 McNuggets. In other words, show how, given solutions for 50-55, > > one can derive solutions for 56-65. > > Theorem: If it is possible to buy x, x+1,…, x+5 sets of McNuggets, for > some > > x, then it is possible to buy any number of McNuggets >= x, given that > > McNuggets come in 6, 9 and 20 packs. > > > > Here is my code: > > for a in range(1,10): > > for b in range(1,5): > > for c in range(1,5): > > mc=(6*a)+(9*b)+(20*c) > > if mc==50: > > print a,b,c > > else: > > print a,b,c > > a=+1 > > b=b+1 > > c=c+1 > > Welcome to the list. > > You need to format your code correctly for anyone to help you. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem
Can you please rewrite the python code using proper indentation, since, as you know, indentation matters in Python Le 28 août 2016 18:40, "shahan khan" a écrit : Hello I'm teching myself Python using MIT opencourse ware. I'm a beginner and have some what knowledge of c and c++. I'm using Python version Here is the problem: McDiophantine: Selling McNuggets In mathematics, a Diophantine equation (named for Diophantus of Alexandria, a third century Greek mathematician) is a polynomial equation where the variables can only take on integer values. Although you may not realize it, you have seen Diophantine equations before: one of the most famous Diophantine equations is: xn + yn= zn. For n=2, there are infinitely many solutions (values for x, y and z) called the Pythagorean triples, e.g. 32 + 42 = 52. For larger values of n, Fermat’s famous “last theorem” states that there do not exist any positive integer solutions for x, y and z that satisfy this equation. For centuries, mathematicians have studied different Diophantine equations; besides Fermat’s last theorem, some famous ones include Pell’s equation, and the Erdos-Strauss conjecture. For more information on this intriguing branch of mathematics, you may find the Wikipedia article of interest. We are not certain that McDonald’s knows about Diophantine equations (actually we doubt that they do), but they use them! McDonald’s sells Chicken McNuggets in packages of 6, 9 or 20 McNuggets. Thus, it is possible, for example, to buy exactly 15 McNuggets (with one package of 6 and a second package of 9), but it is not possible to buy exactly 16 nuggets, since no non-negative integer combination of 6’s, 9’s and 20’s adds up to 16. To determine if it is possible to buy exactly n McNuggets, one has to solve a Diophantine equation: find non-negative integer values of a, b, and c, such that 6a + 9b + 20c = n. Problem 1. Show that it is possible to buy exactly 50, 51, 52, 53, 54, and 55 McNuggets, by finding solutions to the Diophantine equation. You can solve this in your head, using paper and pencil, or writing a program. However you chose to solve this problem, list the combinations of 6, 9 and 20 packs of McNuggets you need to buy in order to get each of the exact amounts. Given that it is possible to buy sets of 50, 51, 52, 53, 54 or 55 McNuggets by combinations of 6, 9 and 20 packs, show that it is possible to buy 56, 57,…, 65 McNuggets. In other words, show how, given solutions for 50-55, one can derive solutions for 56-65. Theorem: If it is possible to buy x, x+1,…, x+5 sets of McNuggets, for some x, then it is possible to buy any number of McNuggets >= x, given that McNuggets come in 6, 9 and 20 packs. Here is my code: for a in range(1,10): for b in range(1,5): for c in range(1,5): mc=(6*a)+(9*b)+(20*c) if mc==50: print a,b,c else: print a,b,c a=+1 b=b+1 c=c+1 and this is the output: 1 1 1 1 2 2 1 3 3 1 4 4 1 2 1 1 3 2 1 4 3 1 5 4 1 3 1 1 4 2 1 5 3 1 6 4 1 4 1 1 5 2 1 6 3 1 7 4 2 1 1 1 2 2 1 3 3 1 4 4 1 2 1 1 3 2 1 4 3 1 5 4 1 3 1 1 4 2 1 5 3 1 6 4 1 4 1 1 5 2 1 6 3 1 7 4 3 1 1 1 2 2 1 3 3 1 4 4 1 2 1 1 3 2 1 4 3 1 5 4 1 3 1 1 4 2 1 5 3 1 6 4 1 4 1 1 5 2 1 6 3 1 7 4 4 1 1 1 2 2 1 3 3 1 4 4 1 2 1 1 3 2 1 4 3 1 5 4 1 3 1 1 4 2 1 5 3 1 6 4 1 4 1 1 5 2 1 6 3 1 7 4 5 1 1 1 2 2 1 3 3 1 4 4 1 2 1 1 3 2 1 4 3 1 5 4 1 3 1 1 4 2 1 5 3 1 6 4 1 4 1 1 5 2 1 6 3 1 7 4 6 1 1 1 2 2 1 3 3 1 4 4 1 2 1 1 3 2 1 4 3 1 5 4 1 3 1 1 4 2 1 5 3 1 6 4 1 4 1 1 5 2 1 6 3 1 7 4 7 1 1 1 2 2 1 3 3 1 4 4 1 2 1 1 3 2 1 4 3 1 5 4 1 3 1 1 4 2 1 5 3 1 6 4 1 4 1 1 5 2 1 6 3 1 7 4 8 1 1 1 2 2 1 3 3 1 4 4 1 2 1 1 3 2 1 4 3 1 5 4 1 3 1 1 4 2 1 5 3 1 6 4 1 4 1 1 5 2 1 6 3 1 7 4 9 1 1 1 2 2 1 3 3 1 4 4 1 2 1 1 3 2 1 4 3 1 5 4 1 3 1 1 4 2 1 5 3 1 6 4 1 4 1 1 5 2 1 6 3 1 7 4. Can someone please tell me whats wrong with my code?. I'm using for loops to give possible valutes to a,b and c and then using the equation (6*a)+(9*b)+(20*c) to determine possible values for a,b and c for satisfying the equation. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem
I understand your argument. You're saying M(50),M(51)and M(52) is basically a set of 6 9 and 20 packs and we should approach it by using lower values starting from 0 for one or two variables to simply the solution on paper. I think I have some idea now as to how to approach this problem. Sent from my iPhone > On 29-Aug-2016, at 5:18 AM, Danny Yoo wrote: > >> On Sun, Aug 28, 2016 at 7:46 AM, shahan khan wrote: >> Hello >> I'm teching myself Python using MIT opencourse ware. I'm a beginner and >> have some what knowledge of c and c++. I'm using Python version > > > >> Here is my code: > [code cut] > > Before showing code, try to express what you're trying to do in > English first. That is, your explanation near the bottom: > >> I'm using for loops to give possible valutes to a,b and c and then using the >> equation (6*a)+(9*b)+(20*c) to determine possible values for a,b and c for > satisfying the equation. > > ... this should actually be presented because it's the most important > part! The problem with code is that code just executes. Any > expectations we have about what that code does is a matter of human > interpretation. > > > > Anyway, from looking at your original code (modulo indentation), I > *think* the treatment as a search problem, looking for the values of > the parameters a, b, c is reasonable, with the way you're doing nested > for loops, > >> for a in range(1,10): >> for b in range(1,5): >> for c in range(1,5): > > However, I am not entirely convinced that the search bounds are > actually sound. Here's why: I don't know why the numbers 1, 10, 5, > and 5 were chosen in the loop: they seem arbitrary, and you need to > say where those numbers came from. I suspect there's a problem with > them anyway. > > > One concrete reason why I don't think it works: change the problem > slightly. Say that we're trying to find a solution for the parameters > where the Diophantine equation evaluates to 6. Basically, the really > simple edge case. > > What change would you make to your code for this simpler case? I'd > assume that the simplest thing to do would be to change the target > value in the test expression, from: > >if mc==50: ... > > to > >if mc == 6: ... > > > Now, before we even try your program, we *know* there's a trivial > solution for a, b, and c such that the equation sums to six! > >a = 1 >b = 0 >c = 0 > > However, looking back at program structure, we can see, even without > looking at the rest of the code, that it can't possibly consider such > parameters, because it's assuming that all three variables have to be > positive. > > So that's something that we know needs to be fixed in some way. > > > > My take on the problem: this sounds very much like a question begging > for a dynamic programming approach, rather than a nested loop > approach. The reason I say this is because of the part of the problem > statement: > > ### > Theorem: If it is possible to buy x, x+1,…, x+5 sets of McNuggets, for some > x, then it is possible to buy any number of McNuggets >= x, given that > McNuggets come in 6, 9 and 20 packs. > ### > > would be the beginning of a mathematical induction proof, which is the > scent of a dynamic programming problem. > > > I'd sketch this as follows: let's call M(n) a boolean function on the > natural numbers that's true if we can buy n McNuggets by *any* > combination of 6, 9, and 20 packs. > > We know that: > >* M(6) is true >* M(9) is true >* M(20) is true > > Why? Because we can take a single 6-pack, or a single 9-pack, or a > single 20-pack, and each of these is a way to buy 6, 9, or 20 > McNuggets. > > From that core collection of knowledge, that inductive base, we can > start investigating what else we can discover by building upon that > knowledge inductively. > > > Example: we know that since M(6) is true, that M(12) is true, because > we can include another 6-pack to the one that built up M(6). > Furthermore, we know that M(15) is true, because M(6) is true, and we > can include another 9-pack to the one that built up M(6). > > > What about M(50)? Well, not quite sure. However, I do know this: if > M(50) is true, then *at least one* of the following has to be true: > > a. M(44) is true > b. M(41) is true > c. M(30) is true > > How in the world would we know this? > > Because if we were able to make 50 McNuggets out of a collection of 6, > 9, or 20 packs, then we have to have picked one of those packs. And > the remainder, outside of that pack, is where we get 44, 41, or 30. > > We don't have absolute knowledge here: it's *conditional* because we > don't yet know if M(44), M(41), or M(30) is true. But it's a start. > > I don't want to say too much more about this. The reason is because > if I state the idea just a little bit more formall
Re: [Tutor] Problem
if you print the values of a, b ,c that satisfy and don't satisfy the condiction cm == 50 at the same time, you can't know what works and what did not work. here is the code that i wrote and worked well for a in range(1, 11): # i replaced 10 by 11 to include the 10 for b in range(1, 6): # same as before put 6 to include 5 for c in range(1, 6): mc = (6*a) + (9*b) + (20*a) if mc == 50: print(a, b, c) notice that i am using python 3.5, range is a generator like xrange in python 2 and the result is 2 for a and b and 1 for c I wish this coold help you for what you said, here is the hard coded solution: for a in range(1, 11): for b in range(1, 6): for c in range(1, 6): mc = 6*a + 9*b + 20*c if mc == 50: print('for 50 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, c)) if mc == 51: print('for 51 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, c)) if mc == 52: print('for 52 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, c)) if mc == 53: print('for 53 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, c)) if mc == 54: print('for 54 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, c)) if mc == 55: print('for 55 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b, c)) and the result was for 55 McNuggets, "a"= 1, "b"=1, "c"=2 for 53 McNuggets, "a"= 1, "b"=3, "c"=1 for 50 McNuggets, "a"= 2, "b"=2, "c"=1 for 53 McNuggets, "a"= 4, "b"=1, "c"=1 Or you can write a more elegant one: for a in range(1, 11): for b in range(1, 6): for c in range(1, 6): mc = 6*a + 9*b + 20*c if mc in range(50, 56): print('for {} McNuggets, "a"= {}, "b"={}, "c"={}'.format(mc, a, b, c)) i used range(50, 56) to include the value 56 know , i wanted to know the purpose of this code: > a=+1 > b=b+1 > c=c+1 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem
The " {} " is the place holder of the values of a, b and c argument passed to format. this example maybe can help to understand print('I like the python {}'.format('mailing-list')) will output >>> I like the python mailing-list The format methode will substitute the {} with the argument it takes. if you want to insert more than one string in the string 'I like python' you have to use multiple {} example: print('Il like the {} {}'.format('Python', 'mailing-list')) will output >> I like the python mailing-list you can google it under the term of "string formating python" and learn more if you want > 1) why is it printing backwards? it is giving that order of result because of the values that takes a, b, c in across the different iteration. the outer loop over the fisrt iteration gives set a to 1 the middel loop over the fisrt iteration gives set b to 1 the inner loop over the fisrt iteration gives set c to 1 and that dont give any result so the inner loop continue to run until it sets c to 4 witch gives the result of 55. this output is conditioned by the algorythm used > 2) why is it skipping 51,52 and 54? Are you shure those values are possible. Can you give me the values of a, b and c you calculated by hand for those different results? regards ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem
On 28/08/16 23:53, shahan khan wrote: I changed the code a bit and this is the result: for a in range(1,11): for b in range(1,6): for c in range(1,6): mc=(6*a)+(9*b)+(20*c) if mc==50: print 'For 50 McNuggets:''a=',a,'b=',b,'c=',c if mc==51: print 'Fpr 51 McNuggets:''a=',a,'b=',b,'c=',c if mc==52: print 'For 52 McNuggets:''a=',a,'b=',b,'c=',c if mc==53: print 'For 53 McNuggets:''a=',a,'b=',b,'c=',c if mc==54: print 'For 54 McNuggets:''a=',a,'b=',b,'c=',c if mc==55: print 'For 55 McNuggets:''a=',a,'b=',b,'c=',c Result: For 55 McNuggets:a= 1 b= 1 c= 2 For 53 McNuggets:a= 1 b= 3 c= 1 For 50 McNuggets:a= 2 b= 2 c= 1 For 53 McNuggets:a= 4 b= 1 c= 1 Two questions: 1) why is it printing backwards? It is printing in the order it finds them based on your loop values. So the first two have a=1, the 3rd has a=2 and the last a=4 But please post in plain text not HTML since otherwise your code format gets messed up. Alan G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem
The following code should do. for a in range(1,10): for b in range(1,5): for c in range(1,5): for mc in range(50, 55): if mc ==(6*a)+(9*b)+(20*c): print "mc= ",mc,"a= ",a,"b= ",b,"c=",c regards, Sarma. On Mon, Aug 29, 2016 at 3:34 PM, Alan Gauld via Tutor wrote: > On 28/08/16 23:53, shahan khan wrote: >> >> I changed the code a bit and this is the result: >> for a in range(1,11): >> for b in range(1,6): >> for c in range(1,6): >> mc=(6*a)+(9*b)+(20*c) >> if mc==50: >> print 'For 50 McNuggets:''a=',a,'b=',b,'c=',c >> if mc==51: >> print 'Fpr 51 McNuggets:''a=',a,'b=',b,'c=',c >> if mc==52: >> print 'For 52 McNuggets:''a=',a,'b=',b,'c=',c >> if mc==53: >> print 'For 53 McNuggets:''a=',a,'b=',b,'c=',c >> if mc==54: >> print 'For 54 McNuggets:''a=',a,'b=',b,'c=',c >> if mc==55: >> print 'For 55 McNuggets:''a=',a,'b=',b,'c=',c >> Result: >> For 55 McNuggets:a= 1 b= 1 c= 2 >> For 53 McNuggets:a= 1 b= 3 c= 1 >> For 50 McNuggets:a= 2 b= 2 c= 1 >> For 53 McNuggets:a= 4 b= 1 c= 1 >> >> Two questions: >> 1) why is it printing backwards? > > > It is printing in the order it finds them based on your loop values. > So the first two have a=1, the 3rd has a=2 and the last a=4 > > But please post in plain text not HTML since otherwise your code > format gets messed up. > > Alan G > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] __init__
Hi: If __init__ is not defined in a class, will it be called when creating an instance? What in a case if this class inherits from parent with __init__ and without __init__? Thank you Monika www.theictm.org (Sponsored by Content.Ad) 1 Fruit That "Destroys" Diabetes http://thirdpartyoffers.netzero.net/TGL3241/57c4872872749728513dst03duc ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] super
Hi: Why in super(Child,self).setVal(value) there is Child and self in super? Why the repetition? Or should it say Parent instead of Child? In my understanding it should say Parent2 instead of Child since it refers to which parent to take the medthod from. In case of multiple inheritance it should state the class from which super will take the setVal method. Please explain what Im missing here. Thank you Monika class Paren2t(): def setVal(self, value): self.value = value class Child(Parent1, Parent2): def setVal(self, value) super(Child,self).setVal(value) legitfeed.com (Sponsored by Content.Ad) 10 Disturbing Things Your Nails Reveal About Your Health http://thirdpartyoffers.netzero.net/TGL3241/57c4a6dd3b01026dd4006st03duc ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] __init__
On 29/08/16 20:03, monik...@netzero.net wrote: If __init__ is not defined in a class, will it be called when creating an instance? What in a case if this class inherits from parent with __init__ and without __init__? The easiest way to find out is try it and see what happens! Just put appropriate print statements in the method. Alan G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] __init__
On Mon, Aug 29, 2016 at 3:03 PM, monik...@netzero.net wrote: > > > Hi: > If __init__ is not defined in a class, will it be called when creating an > instance? > What in a case if this class inherits from parent with __init__ and without > __init__? > Thank you > Monika > > www.theictm.org (Sponsored by Content.Ad) > 1 Fruit That "Destroys" Diabetes > http://thirdpartyoffers.netzero.net/TGL3241/57c4872872749728513dst03duc > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor The class will be instantiated.. but no attributes will be set -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] super
On Mon, Aug 29, 2016 at 09:18:17PM +, monik...@netzero.net wrote: > Hi: > Why in super(Child,self).setVal(value) there is Child and self in super? Why > the repetition? Or should it say Parent instead of Child? > In my understanding it should say Parent2 instead of Child since it refers to > which parent to take the medthod from. In case of multiple inheritance it > should state the class from which super will take the setVal method. > Please explain what Im missing here. A lot :-) No, super() doesn't specify which parent to take the message from, because in multiple inheritence you normally need to take it from ALL parents: class Child(Parent1, Parent2): def setVal(self, value): Parent1.setVal(self, value) Parent2.setVal(self, value) If you're writing that, then super() is a better solution. def Child(Parent1, Parent2): def setVal(self, value): # This will automatically call both Parents super(Child, self).setVal(val) It is better because in complex class hierarchies that form a diamond shape, calling each parent by hand may cause one of the methods to be called twice. super() will avoid that. What if the method is only defined once? Let's say that Parent1 defines setVal, but Parent2 doesn't. Or worse, you're not sure whether they both have a setVal method -- at least one does, for sure, but maybe both of them do, and you're not sure which. super() to the rescue. If you always use super(), it will ensure that the method is called from each parent that has it, but not from parents that don't. (So long as at least one parent has the method.) It might help to see what Raymond Hettinger has said about super: https://rhettinger.wordpress.com/2011/05/26/super-considered-super/ Or if you prefer a video: https://www.youtube.com/watch?v=EiOglTERPEo And remember, in Python 3 (but not Python 2) you can just call super() with no arguments, and the interpreter will work out what you mean. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] __init__
On Mon, Aug 29, 2016 at 07:03:31PM +, monik...@netzero.net wrote: > > > Hi: > If __init__ is not defined in a class, will it be called when creating an > instance? Yes, because the default __init__ does nothing. So if you don't need an __init__, just don't bother to write it! Try experimenting with code like this at the interactive interpreter: # Wrong, don't do this: class MyObject(): def __init__(self): # Do nothing! pass def method(self): print("called method") # Better: class MyObject(): def method(self): print("called method") obj = MyObject() > What in a case if this class inherits from parent with __init__ and without > __init__? If you use super(), Python will work out what to do. So long as all the existing __init__ methods take the same arguments, use super(). If they take different arguments, you can't use super() and have to do it by hand. I see you are doing a lot of multiple inheritence (MI). I don't want to discourage you, but MI is considered an advanced subject. (Most programming languages don't even support it!) You should make sure you understand single inheritence and super() very well before tackling MI, otherwise you're likely to get confused. In any case, feel free to ask questions or ask for clarifications, and we'll answer as best we can. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor