[Tutor] Pease help

Alan Gauld alan.gauld at btinternet.com
Sat Jun 7 10:50:56 CEST 2014


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



More information about the Tutor mailing list