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 <shahankh...@gmail.com> 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 <zemmoura.kha...@gmail.com> 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