Re: [Tutor] Pease help

2014-06-07 Thread Alan Gauld

On 07/06/14 00:00, Glen Chan wrote:

Hello I am a student trying to figure out this program. Here is my
objective and program below. What am I doing wrong? I can't get this to
work.



Thats not a very helpful description of the problem.
Tell us what you get. Include any error messages- the full
text not a summary.

Also tell us the Python version you are using and the OS.
In this case it looks like you are using Python v2.x
but if you are trying to ru it in python3 that could
be part of your problem.


#the main function

def main():
endProgram = 'no'
print
while endProgram == 'no':
option = 0
print
print 'Enter 1 to enter in new data and store to file'
print 'Enter 2 to display data from the file'
option = input('Enter now -')
print


Unfortunately you posted using HTML which loses all the formatting.
Find out how to post using plain text, it makes life much easier.

However there are some general comments below:


# declare variables

pints = [0] * 7
totalPints = 0
averagePints = 0
if option == 1:

# function calls

pints = getPints(pints)
totalPints = getTotal(pints, totalPints)
averagePints = getAverage(totalPints, averagePints)

else:


endProgram = raw_input('Do you want to end program? (Enter no or yes): ')


while not (endProgram == 'yes' or endProgram == 'no'):

print 'Please enter a yes or no'
endProgram = raw_input('Do you want to end program? (Enter no or yes): ')



This loop doesn't seem to do much. It gets a yes/no value
from the user but you don't do anything with that value?


def getPints(pints):
counter = 0
while counter  7:
pints[counter] = input('Enter pints collected: ')
counter = counter + 1
return pints


You could do that more easily with a for loop.

for counter in range(7):
   pints[counter] = input(...)
return pints

If you have covered list comprehensions you could do it
in a single line! But I'm guessing thats a step too far
for now.

However using input() is a bad idea. input() executes the input
as if it were Python code. That's not secure because users can
(deliberately or accidentally) enter damaging code which could, for 
example, result in your hard disk being deleted. It's better to use 
raw_input() for everything and convert the data from a string using 
int() or float() etc.



def getTotal(pints, totalPints):
counter = 0
while counter  7:
totalPints = totalPints + pints[counter]
counter = counter + 1
return totalPints


You can use the built-in sum() function to add all the
elements in a collection

totalPints = sum(pints)


def getAverage(totalPints, averagePints):
averagePints = float(totalPints) / 7
return averagePints



def writeToFile(averagePints, pints):


You don't have any code here? Not even a pass statement?


#the readFromFile function
def readFromFile(averagePints, pints):


Same here.
If you don't have any code to put in the body insert
either a pass or an empty return statement. You can then
replace it with your real code later, but at least
Python will be happy.


main()


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pease help

2014-06-07 Thread Cameron Simpson

On 06Jun2014 19:00, Glen Chan gchan...@msn.com wrote:

Hello I am a student trying to figure out this program. Here is my objective 
and program below. What am I doing wrong? I can't get this to work.


Hi Glen,

First, please configure your mailer to send plain text instead of HTML. It 
works better for everyone, especially with program code.


Next, what does it do? You've supplied the requirements, fairly obviously a 
homework or tutorial exercise.


However, you need to describe what you expected your program to do, and what it 
appears to do instead. Always try to include the output of the failing run so 
that people can look at it and make suggestions.


Meanwhile, I'll make a few remarks about the program itself, inline below.


def main():
 endProgram = 'no'


endProgram looks like a Boolean variable. Use a Boolean value with it:

  endProgram = False


 print
 while endProgram == 'no':


And if you set endProgram as above, you can write this as:

while not endProgram:


   option = 0
   print
   print 'Enter 1 to enter in new data and
store to file'
   print 'Enter 2 to display data from the
file'
   option = input('Enter now -')


Based on the print statements above, I'm presuming you are using Python 2.

Don't use input, use raw_input. input treats the input as a Python 
expression, and can do all sorts of horrible and dangerous things.


Instead, if optiuon is supposed to be an integer, do this:

  option = int(raw_input('Enter now -'))

[...]

   endProgram = raw_input('Do you want to end program? (Enter no or yes): ')
   while not (endProgram == 'yes' or endProgram == 'no'):
 print 'Please enter a yes or no'

[...]

Based on the suggested change earlier, this loop should then be followed by:

endProgram = (endProgram == 'yes')

to get a Boolean value for the loop control variable endProgram.

Cheers,
Cameron Simpson c...@zip.com.au
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Pease help

2014-06-06 Thread Glen Chan
Hello I am a student trying to figure out this program. Here is my objective 
and program below. What am I doing wrong? I can't get this to work.
 



Calculate the average pints of
blood donated during a blood drive.  The
program should take in the number of pints donated during the drive, based on a
seven hour drive period.  The average
pints donated during that period should be calculated and written to a
file.  Write a loop around the program to
run multiple times.  The data should be
appended to the file to keep track of multiple days.  If the user wants to 
print data from the
file, read it in and then display it. 
Store the pints per hour and the average pints donated in a file called
blood.txt.  





 #the
main function

def
main():


  endProgram = 'no'


  print


  while endProgram == 'no':


option = 0


print


print 'Enter 1 to enter in new data and
store to file'


print 'Enter 2 to display data from the
file'


option = input('Enter now -')


print


 
# declare variables

pints = [0] * 7


totalPints = 0


averagePints = 0


if option == 1:



  # function calls

  pints = getPints(pints)


  totalPints = getTotal(pints, totalPints)


  averagePints = getAverage(totalPints,
averagePints)


  
else:


endProgram = raw_input('Do you want to end
program? (Enter no or yes): ')


while not (endProgram == 'yes' or
endProgram == 'no'):

  print 'Please enter a yes or no'


  endProgram = raw_input('Do you want to
end program? (Enter no or yes): ')



 #the
getPints function

def
getPints(pints):


  counter = 0


  while counter  7:


  pints[counter] = input('Enter pints
collected: ')


  counter = counter + 1


  return pints


 
#the
getTotal function

def
getTotal(pints, totalPints):


  counter = 0


  while counter  7:


totalPints = totalPints + pints[counter]


counter = counter + 1


  return totalPints



 #the
getAverage function

def
getAverage(totalPints, averagePints):


  averagePints = float(totalPints) / 7


  return averagePints






#the
writeToFile function

def
writeToFile(averagePints, pints):





#the
readFromFile function


 def readFromFile(averagePints, pints):



#
calls main


main()


  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor