Program prints questions for user input, but won't show the answer output

Steven D'Aprano steve at pearwood.info
Wed May 18 22:14:50 EDT 2016


On Thu, 19 May 2016 11:50 am, Jake Kobs wrote:

> MRAB,
> 
> I am not quite sure how to return the print statements so I thought that
> returning the displayInfo def would help.. Im so lost.

There's no need to return the text that you printed. You printed it, the job
is done.


Here is some general advice that may help you be less lost.

(1) Each function should do "one thing". For example, it either collects
information, or it displays it. It shouldn't do both. Your code seems
pretty good at following that guideline already, well done.

(2) Use existing functions as much as possible. The Python docs has a list
of the built-in functions here:

https://docs.python.org/2/library/functions.html

Refer to that as often as possible.

For example, your functions getHigh, getLow and getTotal functions just
re-invent the wheel. Python already has functions to do that:

max, min, sum

In Python 2, there's no built-in function for average, but for your purposes
you can just use sum(pints)/len(pints).

(3) Whoever taught you to write while loops should be taken out and
horse-whipped. While loops are for looping when you don't know in advance
how many times you need to repeat. When you do know how many times to
repeat, use a for-loop:

# I want to repeat seven times
for counter in range(7):
    print "Loop", counter


Isn't that simpler than a while loop?

# I want to repeat seven times
counter = 7
while counter > 0:  # or should that be >= 0? >= 1? I forget!
    print "Loop", counter
    counter -= 1


And its faster too.


(4) Don't use "input". (The reasons are technical, if you really want to
know, please ask.) Instead of writing:

    input("Enter pints collected: ")

write this:

    int(raw_input("Enter pints collected: "))




-- 
Steven




More information about the Python-list mailing list