solving a small programm

H.V sulfugor at rocketmail.com
Sun Jan 11 11:12:47 EST 2004


"broebel" <computer.problemen at skynet.be> wrote in message news:<40004254$0$16669$ba620e4c at news.skynet.be>...
> hey,
> 
> for the real programmers amongst you, this may be really annoying but I've
> been learning the language for only two days.
> 
> this is my problem,
> in this programm,which already works I now have to make a total count of how
> many coins are used.
> this program gives the total per coin. It should be a small peace of code
> (as explained in a tutorial i read, without the answer.)that counts the
> total of all the coins.
> I ve been searching for two days and for this kind of programm it really
> seems kind of long.
> 
> thanks in advance
> 
> # Een bedrag gepast betalen met zo min mogelijk euromunten
> 
> bedrag = input ( 'Geef bedrag tussen 0 en 500 eurocent: ' )
> 
> for munt in 200, 100, 50, 20, 10, 5, 2, 1 :
>     aantal = 0
> 
>     while bedrag >= munt :
>         aantal = aantal + 1
>         bedrag = bedrag - munt
> 
>     if aantal > 0 :
>         print aantal, 'x', munt
> 
> what i need is:
> for example
> the programm gives
> 68= 50*1
>        10*1
>        5*1
>        2*1
>        1*1
> I need an additional code to get the "5" total of all the coins together.
--
An alternative to keeping track of coins as you go would be to store
your results in some kind of data structure : use a dict whose keys
are the coin type and whose values ate the number of each type of coin
needed. Then you can just add all the "values" . I like the data
centric approach . Your code doesn't seem to exploit any of the
convenient data structures at all .

sulf



More information about the Python-list mailing list