3 Replies - 269 Views - Last Post: 21 January 2013 - 10:48 PM Rate Topic: -----

#1 psk102000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-January 13

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

Posted 21 January 2013 - 07:41 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Returning string in form of donut 1,donut 2,donut 3

#2 psk102000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-January 13

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

Posted 21 January 2013 - 10:39 PM

I solved it on my own.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

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

Posted 21 January 2013 - 10:40 PM

Glad you figured it out! Would you mind sharing your solution so others who view this thread could learn from it? :)
Was This Post Helpful? 0
  • +
  • -

#4 woooee  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 68
  • Joined: 21-November 12

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

Posted 21 January 2013 - 10:48 PM

See the same post here for hints as to the solution.

This post has been edited by woooee: 21 January 2013 - 10:48 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1