Ok, uhh... I need help. My loop to keep the program running or to quit isn't working right. Can somebody help me figure it out? I tried a couple of methods, and they didn't work. Do anybody have any idea? By the way, this is for a blood drive program I'm writing... Here's the code:

#The main() function:
def main():
    endProgram = "no"
    print
    while endProgram == "no":
        print
        # Declare Variables Here:
        pints = [0] * 7
        pintsTotal = 0
        pintsAvg = 0
        pintsHigh = 0
        pintsLow = 0
        # Function Calls Here:
        pints = getPints(pints)
        pintsTotal = getTotal(pints, pintsTotal)
        pintsAvg = getAvg(pintsTotal, pintsAvg)
        pintsHigh = getHigh(pints, pintsHigh)
        pintsLow = getLow(pints, pintsLow)
        info = dispInfo(pintsTotal, pintsAvg, pintsHigh, pintsLow)
        endProgram = raw_input ("Do you want to quit the program? Yes or No?: ")
        while (endProgram != "yes" or endProgram != "no"):
            print "Please enter a yes or no!"
            return endProgram == raw_input ("Do you want to quit the program? Yes or No?: ")
#The getPints(pints) function:
def getPints(pints):
    counter = 0
    while counter < 7:
        pints[counter] = input ("Enter pints collected: ")
        counter = counter + 1
    return pints
#The getTotal(pints, pintsTotal) function:
def getTotal(pints, pintsTotal):
    counter = 0
    while counter < 7:
        pintsTotal = pintsTotal + pints[counter]
        counter = counter + 1
    return pintsTotal
#The getAvg(pintsTotal, pintsAvg) function:
def getAvg(pintsTotal, pintsAvg):
    pintsAvg = pintsTotal / 7
    return pintsAvg
#The getHigh(pints, pintsHigh) function:
def getHigh(pints, pintsHigh):
    pintsHigh = pints[0]
    counter = 1
    while counter < 7:
        if pints[counter] > pintsHigh:
            pintsHigh = pints[counter]
            counter = counter + 1
        return pintsHigh
#The getLow(pints, pintsLow) function:
def getLow(pints, pintsLow):
    pintsLow = pints[0]
    counter = 1
    while counter < 7:
        if pints[counter] < pintsLow:
            pintsLow = pints[counter]
            counter = counter + 1
        return pintsLow
#Function to display the information:
def dispInfo(pintsTotal, pintsAvg, pintsHigh, pintsLow):
    print "The total number of pints donated was: ", pintsTotal
    print "The highest number of pints donated was: ", pintsHigh
    print "The lowest number of pints donated was: ", pintsLow
    print "The average # of pints donated was: ", pintsAvg
    return

main()

Basically. You have 7 iterations you must enter for pints of blood, or blood donations. Then, it calculates the total, average, and the high and low amount of blood donations. This type of thing could possibly be useful for when you are doing blood donations. The only thing is, my loop doesn't work correctly. That why I posted it... To see if anybody can figure out how to get it to work correctly.

You could simplify main() a little to make the yes/no exit work ...

def main():
    print
    while True:
        print
        # Declare Variables Here:
        pints = [0] * 7
        pintsTotal = 0
        pintsAvg = 0
        pintsHigh = 0
        pintsLow = 0
        # Function Calls Here:
        pints = getPints(pints)
        pintsTotal = getTotal(pints, pintsTotal)
        pintsAvg = getAvg(pintsTotal, pintsAvg)
        pintsHigh = getHigh(pints, pintsHigh)
        pintsLow = getLow(pints, pintsLow)
        info = dispInfo(pintsTotal, pintsAvg, pintsHigh, pintsLow)

        endProgram = raw_input ("Do you want to quit the program? Yes or No?: ")
        if 'n' in endProgram.lower():
            break

Have make a better menu system.
Put all comment in doc string.

Look at exception handling for your function.
Try type a letter.

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

def getTotal(pints, pintsTotal):
    '''The getTotal(pints, pintsTotal) function:'''
    counter = 0
    while counter < 7:
        pintsTotal = pintsTotal + pints[counter]
        counter = counter + 1
    return pintsTotal

def getAvg(pintsTotal, pintsAvg):
    '''The getAvg(pintsTotal, pintsAvg) function:'''
    pintsAvg = pintsTotal / 7
    return pintsAvg

def getHigh(pints, pintsHigh):
    '''The getHigh(pints, pintsHigh) function:'''
    pintsHigh = pints[0]
    counter = 1
    while counter < 7:
        if pints[counter] > pintsHigh:
            pintsHigh = pints[counter]
            counter = counter + 1
        return pintsHigh

def getLow(pints, pintsLow):
    '''The getLow(pints, pintsLow) function:'''
    pintsLow = pints[0]
    counter = 1
    while counter < 7:
        if pints[counter] < pintsLow:
            pintsLow = pints[counter]
            counter = counter + 1
        return pintsLow

def dispInfo(pintsTotal, pintsAvg, pintsHigh, pintsLow):
    '''Function to display the information:'''
    print "The total number of pints donated was: ", pintsTotal
    print "The highest number of pints donated was: ", pintsHigh
    print "The lowest number of pints donated was: ", pintsLow
    print "The average # of pints donated was %.2f " % (pintsAvg) #display float with 2 decimal
    return

def main():
    '''Main menu and info '''            
    while True:               
        print 'Welcome to my menu\n'
        print '(1) Calculate pints'
        print '(2) Display pints info'
        print '(q) Quit' 
        choice = raw_input('Enter your choice: ') 
        if choice == '1':
            # Declare Variables Here:
            pints = [0] * 7
            pintsTotal = 0
            pintsAvg = 0
            pintsHigh = 0
            pintsLow = 0          
            pints = getPints(pints)
            pintsTotal = getTotal(pints, pintsTotal)
            pintsAvg = getAvg(pintsTotal, pintsAvg)
            pintsHigh = getHigh(pints, pintsHigh)
            pintsLow = getLow(pints, pintsLow)            
        elif choice == '2': 
            try:
                info = dispInfo(pintsTotal,pintsAvg,pintsHigh,pintsLow )
            except:  #find error not god to only use "except"
                print 'No info yet'
        elif choice == 'q': 
            exit() 
        else: 
            print 'Not a correct choice:', choice
            
if __name__ == '__main__':
    main()

Edited 6 Years Ago by snippsat: n/a

Thanks, snippsat. That works just the way it needs to... :) Also, I'm going to re-post this code with a credits function. So anybody who uses it knows who made it. And, I'm going to include you in the credits, since you helped fix the program. Thanks for the help, again!! :)

This question has already been answered. Start a new discussion instead.