user2006236
user2006236

Reputation: 85

Python Word Prob Issue

Here's the word problem: It takes exactly 2 minutes and 7 seconds to produce an item. Unfortunately, after 143 items are produced, the fabricator must cool off for 5 minutes and 13 seconds before it can continue. Write a program that will calculate the amount of time required to manufacture a given number of items.

The test number is 1340 items.

numItems = 1340
produceitem = 2 * 60 + 7  #2 minutes and 7 seconds
cooldown = 5 * 60 + 13 #5 minutes and 13 seconds
items_before_delay = 143
productiontime = 0

if numItems <= 143:
    productiontime = produceitem * numItems
if numItems > 143:
    productiontime = (produceitems * numItems) - (numItems / items_before_delay * cooldown) 
print str(productiontime) + "seconds"

The output for the test number is supposed to be 172997 seconds but my program outputs it as 167363 seconds.

Can anyone let me know what I can do to improve this?

Upvotes: 2

Views: 74

Answers (1)

abarnert
abarnert

Reputation: 365975

You're subtracting the cooldown time, instead of adding it. That's it.

So, change this:

productiontime = (produceitems * numItems) - (numItems / items_before_delay * cooldown) 

… to this:

productiontime = (produceitems * numItems) + (numItems / items_before_delay * cooldown) 

However, while we're here:

  • You defined produceitem, but used produceitems. If this worked at all, it's probably because you got lucky in the interactive interpreter, having already defined produceitems as well.
  • If you're going to define a constant items_before_delay, don't use the number 143 directly, use items_before_delay.
  • Don't do if a <= b: then if a > b:; just change the second one to else:.
  • In fact, you don't need the if at all. If numItems <= 143, (numitems / items_before_delay * cooldown) will be 0, so the second version will still give the right answer.
  • Unless you're dealing with pretty old versions of Python, it's generally better to explicitly use // for truncating integer division than /. That means your code still works in Python 3.x, or if someone does a __future__ statement, etc.—and, more importantly, it means humans can read and understand your code without having to guess whether it was for 2.x or 3.x.
  • Use a consistent style for your names. items_before_delay follows PEP8 recommendations, but numItems does not.
  • There's no need to "declare" a variable like productiontime before setting it.
  • Concatenating two strings doesn't give you a space in between, and you probably didn't want 172997seconds without a space.
  • Try to avoid writing lines too long to fit in 80 columns. Even if you think nobody cares about old-fashioned text editors, it's still a problem with new-fangled web interfaces like StackOverflow. (Nobody likes unnecessary horizontal scrollbars.)

So:

num_items = 1340
produce_item = 2 * 60 + 7  #2 minutes and 7 seconds
cooldown = 5 * 60 + 13 #5 minutes and 13 seconds
items_before_delay = 143

total_cooldown = num_items // items_before_delay * cooldown
production_time = (produce_item * num_items) + total_cooldown
print '{} seconds'.format(production_time)

Upvotes: 2

Related Questions