Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Returning string in form of donut 1,donut 2,donut 3

$
0
0
Given an int count of a number of donuts, return a string of the form 'donut 1,donut 2,..., donut <count>', where <count> is the number passed in. However, if the count is 5 or more, then use the string 'and <num> more donuts' as the last item instead of the actual list of donuts, where <num> is the number of remaining donuts.

So
donuts(3) returns 'donut 1,donut 2,donut 3'
donuts(4) returns 'donut 1,donut 2,donut 3,donut 4'
donuts(10) returns 'donut 1,donut 2,donut 3,donut 4,and 6 more donuts'
donuts(20) also returns 'donut 1,donut 2,donut 3,donut 4,and 16 more donuts'

Python functions: range(), str()

This is what I have so far but it does not produce the results I want correctly.

def donuts(count):
  if count >= 5:
    count -= 4
    numDonuts = 'and ' + str(count) + ' more donuts'
  else:
    numDonuts = 'donut ' + str(count)
  return numDonuts



I get:

'donut 1'
'donut 3'
'donut 4'
'and 6 more donuts'
'and 95 more donuts'

but I need this:

output
‘donut 1’
‘donut 1,donut 2,donut 3’
‘donut 1,donut 2,donut 3,donut 4’
‘donut 1,donut 2,donut 3,donut 4 and 6 more donuts’
‘donut 1,donut 2,donut 3,donut 4 and 95 more donuts’

I tried to implement the range function but I still can't get it to say donut1,donut2,donut3 in that order.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>