Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics All Together Now Cleaner Code Through Refactoring

Stephen Cole
PLUS
Stephen Cole
Courses Plus Student 15,809 Points

My way of dealing with grammar/plurals.

When I refactored the price calculation, I decided that I wanted to have the English match the amounts. That is, 1 ticket would cost $10 and 2 tickets would cost $20. This was my approach. I thought it to be both DRY and novel.

def calc_price(ticket_request):
  total_cost = ticket_request * TICKET_PRICE

  if num_tickets == 1:
    plural = ''
  else:
    plural = 's'

  print('{} ticket{} costs {} dollars'.format(num_tickets, plural, total_cost))
  print('There is a service charge of ${} per transaction.'.format(SERVICE_CHARGE))

  return total_cost + SERVICE_CHARGE

I'm not sure that I care about dollar vs dollars as I can't see a use case where something would cost a single dollar. If it did, it'd happen so rarely that I'd probably not care about the attention to detail.

That's a fine touch to the code. I really liked it! Thanks for sharing!!

Nice. You could even trim this down if you wanted.

 plural = ''
 if num_tickets > 1: plural = 's'
zoltans
zoltans
2,547 Points

This was my way of thinking:

while tickets_remaining >= 1:

    if tickets_remaining == 1:
        print("There is {} ticket remaining.".format(tickets_remaining))
    else:
        print("There are {} tickets remaining.".format(tickets_remaining))

Is this okay?