[Tutor] Problem
Shahan Khan
shahankhan0 at gmail.com
Sun Aug 28 18:21:25 EDT 2016
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.khalil at 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
More information about the Tutor
mailing list