Lab 7.4- Arrays and Python Code (Optional) Critical Review In Python, arrays are
ID: 3601930 • Letter: L
Question
Lab 7.4- Arrays and Python Code (Optional) Critical Review In Python, arrays are native objects called lists List index starts at 0 (unlike Raptor). The following is a method used when you kow the elements of the array even number [2, 4, 6, 8, 10] You can use the print statement to display an entire list, as shown here: print (even nunbers) The follorwing is a method used when you do not know what the elaments should be, but you lnow the size. numbers= [0] * 5 A loop can also be used to print the elements of the aray. An example is as follows: A for in loop for n in numbers print (n A while loop index = 0 while index 5: print (numbers [index]) index = index + 1 The goal of this lab is to conrert the blood drive program from Lab 7.1 to Pythan code. Step 1: Start the IDLE Environment for Python. Prior to entering code, save your file by clicking on File and then Save. Select your location and save this file as Lab7-4.pp. BExplanation / Answer
Note: I have used raw_input. You can use input function for newer versions of python. below is the code:
def main():
endProgram ='no'
pints = [0] * 7
totalPints = 0
averagePints = 0
highPints = 0
lowPints = 0
while endProgram == 'no':
pints = getPints(pints)
totalPints = getTotal(pints, totalPints)
averagePints = getAverage(totalPints, averagePints)
highPints = getHigh(pints, highPints)
lowPints = getLow(pints, lowPints)
displayInfo(averagePints, lowPints, highPints)
endProgram = raw_input('Do you want to End the program? (Enter yes or no): ')
while not (endProgram == 'yes' or endProgram == 'no'):
print 'Please enter a yes or no'
endProgram = raw_input('Do you want to End the program? (Enter yes or no): ')
def getPints(pints):
counter = 0
while counter < 7:
pints[counter] = raw_input('Enter pints collected: ')
counter = counter + 1
return pints
def getTotal(pints, totalPints):
counter = 0
while counter < 7:
totalPints = totalPints + int(pints[counter])
counter = counter + 1
return totalPints
def getAverage(totalPints, averagePints):
averagePints= totalPints/7
return averagePints
def getHigh(pints, highPints):
counter = 1
highPints = pints[0]
while counter < 7:
if pints[counter] > highPints:
highPints = pints[counter]
counter = counter + 1
return highPints
def getLow(pints, lowPints):
counter = 1
lowPints = pints[0]
while counter < 7:
if pints[counter] < lowPints:
lowPints = pints[counter]
counter = counter + 1
return lowPints
def displayInfo(averagePints, lowPints, highPints):
print "The average number of pints donated is: " + str(averagePints)
print "The highest number of pints donated is: " + str(highPints)
print "The lowest number of pints donated is: " + str(lowPints)
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.