[Tutor] Help! (solution)

michael scott jigenbakuda at yahoo.com
Sat Mar 5 01:15:08 CET 2011


I know that the question has long been answered (and probably due today), but I 
solved it and it was great exercise for me (as I'm not in college at the moment 
and I need assignments like these to gauge my skills). I'll probably build a gui 
for it tomorrow, just so I can practice at that. I wish I had a comp sci lab... 
(T_T) but I digress

But anyways Andrew here is an alternative way to solve the problem (even if it 
is a long and round about method). And to anyone else who is reading beside 
Andrew, if something about my code could be better, please tell me, as this was 
just as much a learning experience for me as it is for Andrew. I need 
constructive criticism at the moment so I don't develop bad habits.


def production_time():
    creation_time = 127
    time_till_rest = 18161
    items = raw_input("How many items will be produced?\n> ")
    item_time = int(items) * creation_time 
    rest_times = item_time/time_till_rest
    print rest_times
    if rest_times > 0:
        total = item_time + (313 * rest_times) #313 is 5 min and 13 secs in 
second form
    else: total = item_time
    time = sec_to_standard(total)
    print "It would take %d days %d hours %d minutes and %d seconds to produce 
that many items" %(time[0], time[1], time[2], time[3])


def sec_to_standard(seconds):
    day = 86400 #secs
    hour = 3600 #secs
    mins = 60#seconds
    creation_time = 127 #secs
    time_till_rest = 18161 #secs
    days = 0
    hours = 0
    minutes = 0
    secs = 0
    if seconds > day:
        while seconds > day:
            print "doing days"
            seconds = seconds - day
            days += 1
    if seconds > hour:
        while seconds > hour:
            print "doing hours"
            seconds = seconds - hour 
            hours += 1
            if hours >= 24:
                days += 1
                hours -= 24
    if seconds > mins:
        while seconds > mins:
            print "doing minutes"
            seconds = seconds - mins
            minutes += 1
            if minutes > 60:
                hours += 1
                minutes -= 60
    secs = seconds
    return days, hours, minutes, secs

production_time()

 ----
What is it about you... that intrigues me so?




________________________________
From: Andrew Bouchot <andy.a.bouchot at gmail.com>
To: tutor at python.org
Sent: Thu, March 3, 2011 4:28:33 PM
Subject: [Tutor] Help!


okay so this is my comp sci lab
 
Problem: ProductionTime.py It takes exactly 2 minutes and 7 second 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. 
Output: Output the amount of time D days HH:MM:SS Sample Input : numItems =1340 
Represents the numbers items to be manufactured Sample Output : 2 days 00:03:17 

 
this is the coding i have written for it!
numitems= int(raw_input("Enter the number of items needed to be manufactured:"))
seconds=numitems*127
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print "%d:%02d:%02d" % (h, m, s)
but how would i add the 5 min and 13 seconds after 143 items have been 
produced???


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110304/0a01cd2a/attachment.html>


More information about the Tutor mailing list