[Tutor] OK here goes nothing

Andy W toodles@yifan.net
Tue, 15 Jan 2002 12:50:45 +0800


> Hi folks, this looks like the place for me.

Hi Don, welcome to the list!

>
> I'm new to programming, new to Python and need tutoring :-) ( I also am an
> old dog trying to learn new tricks )
>
> Anyway here's my first program, feel free to point out what I've done
wrong
> or could do in a more "Pythonic" way.

Sure thing. I'm no expert, but I'll do my best to answer. Just keep going
through the code until you bump into my comment(s).

>
> Thanks for the effort !
>
> Don
>
> #!  /boot/home/config/bin/python
>
> # Logbook 8 day recap
>
> # this program will calculate hours on duty last 7 days, hours avalible
> tomorrow ,hours on duty last 8 days
> # for a truck drivers log book
>
> print
> print " Enter Hours on duty"
> print
>
> # 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 ?')

I'm assuming that this code won't be used by any malicious user anyway, but
do you know the dangers of using input?
If used with Evil Intent, input can be used to damage a system, as it will
evaluate the input data.

>
> # create list to keep track of changing values
>
> historyList = [day1,day2,day3,day4,day5,day6,day7,day8]
> nextDay = 0
> Continue = 0

A common *style* technique for names is using Capitalisation for "classes".
That's completely up to you, though.

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

I'm not sure if you know about the "x in y" statement, nor am I even sure
you want to know about it, but I'll go over it anyway :-)
Say you wanted to also allow for the input of "no", "No" and "N", you can do
the following:

###
while Continue is not in ["n","no","No","N"]
###

However, I'd be more inclined to go for the optimistic approach. ie. check
for the input of "y" ;)
Say if the user input something other than "y" or "n" (or the variations if
you choose to acknowledge them), then with the current code it will be
interpreted as "yes", and the loop will continue. You could either check
that the input is acceptable, or you could check for "y", and anything else
would mean "no".
Maybe that's just one of my quirks.

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

They're both the same, yes?
So you can make it more efficient by evaluating the total once, and then
printing it.
Also, there's a quicker way to add them. (or 2 quicker ways)

### Method One
import operator
reduce(operator.add,historyList[-7:])
###

### Method Two
reduce(lambda a,b:a+b,historyList[-7:])

Have you used the reduce function before? It applies the function (the first
parameter) to each two elements cumulatively.

>
>
> print
> print "total hours avalible  tomorrow:  " , 70 - last7Days
> print
> print "total hours on duty last 8 Days: ", last7Days + historyList [-8]
> print
>
> # output number of days entered
>
> print "You have entered  ",  len (historyList), " Days"
> print
>
> # output list of hours from input
>
> print historyList [0:100]

When slicing a list, it automatically starts at 0 if you leave it blank. ie.
You can say "historyList[:100]".

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

That's all I can think of.
HTH, Andy.

>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>