[Tutor] OK here goes nothing

Pijus Virketis virketis@fas.harvard.edu
Mon, 14 Jan 2002 23:35:04 -0500


Hi, Don!

Well, I am sure you will get a hearty welcome from the gurus on our list ...
Let me just jump right to some suggestions about the code.

> # collect user input for first 8 days of record
>
> day1 = input('Enter total hours on duty day1 ?')
> day2 = input('Enter total hours on duty day2 ?')
> day3 = input('Enter total hours on duty day3 ?')
> day4 = input('Enter total hours on duty day4 ?')
> day5 = input('Enter total hours on duty day5 ?')
> day6 = input('Enter total hours on duty day6 ?')
> day7 = input('Enter total hours on duty day7 ?')
> day8 = input('Enter total hours on duty day8 ?')
>
> # create list to keep track of changing values
>
> historyList = [day1,day2,day3,day4,day5,day6,day7,day8]
> nextDay = 0
> Continue = 0

Clearly, asking for inputs like this is a bit tedious. You had a great idea
with the historyList, so let's use it like so:

historyList = [] #store information about each day here
for i in range(8): #count from 0 up to (but not including!) 8
    querry_string = "Enter total hours on duty on day%d" %(i+1) # generate
the querry strings with the right days
    input = raw_input(querry_string)
    historyList.append(input) #add the input we just got to the list and
start over

>
> # create loop for additional days input
>
> print
> Continue = raw_input ('do you wish to continue adding days ? y/n  : ' )
> while Continue != 'n':
>     print

Hm, I am not quite sure what this print statement does ... Do you just want
to drop down one more line?
In that case, ask for raw_input like this:

Continue = raw_input ('do you wish to continue adding days ? y/n  :\n ' ) #
the \n at the end adds a new line!


>     nextDay = input('enter hours on duty next day :  ')
>     historyList.append(nextDay)
>     print
>     Continue = raw_input ('do you wish to continue adding days? y/n  : ' )
> if Continue == 'n':
>     print
>
> print "                                 RECAP"
> print
"----------------------------------------------------------------------
> ---------"
> print
>
> # output running totals when user is done inputing data
>
> print "total hours on duty last 7 Days: " , historyList [-1 ] +
historyList[-
> 2]+ historyList[-3]+ historyList[-4]\
> + historyList[-5]+ historyList[-6]+ historyList[-7]

Again, this is better handled by a little loop:

for i in historyList:
    totalhours = totalhours + i
print totalhours

>
> # create a variable containing the last 7 days concurrently
>
> last7Days = historyList [-1 ] + historyList[-2]+ historyList[-3]+
> historyList[-4]\
> + historyList[-5]+ historyList[-6]+ historyList[-7]

Well, I guess we already have the totalhours variable. That's what you need,
right?

>
>
> print
> print "total hours avalible  tomorrow:  " , 70 - last7Days
> print
> print "total hours on duty last 8 Days: ", last7Days + historyList [-8]
> print

Whenever you need a new line, add \n rather than using a separate print
statement:

print "\n total hours available tomorrow: "

>
> # output number of days entered
>
> print "You have entered  ",  len (historyList), " Days"
> print
>
> # output list of hours from input
>
> print historyList [0:100]

If you want to do it like this, just say:

print historyList[:] # this gets you the whole list

> print
> print "      Have a Safe Trip :-) "
> print
> print
> print
>

Cheers,

Pijus