这是做作业的。这所特殊的学校提供 0 帮助,而且教授也没有什么帮助。我只是在寻找有关为什么此代码不起作用的指导。我必须使用Python 2.7。当我运行该程序时,它要求我输入适当的品脱数量,但然后什么也不做。
# This program finds the average number of pints collected, the highest amount, and the lowest amount
# Lab 9-4 Blood drive
#the main function
def main():
endProgram = 'no'
print
while endProgram == 'no':
print
# declare variables
pints = [0] * 7
totalPints = 0
averagePints = 0
highPints = 0
lowPints = 0
# function calls
pints = getPints(pints)
totalPints = getTotal(pints, totalPints)
averagePints = getAverage(totalPints, averagePints)
highPints = getHigh(pints, highPints)
lowPints = getLow(pints, lowPints)
displayInfo(averagePints, highPints, lowPints)
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 = totalPints / 7
return averagePints
#the getHigh function
def getHigh(pints, highPints):
highPints = pints[0]
counter = 1
while counter < 7:
if pints[counter] > highPints:
highPints = pints[counter]
counter = counter + 1
return highPints
#the getLow function
def getLow(pints, lowPints):
lowPints = pints[0]
counter = 1
while counter < 7:
if pints[counter] < lowPints:
lowPints = pints[counter]
counter = counter + 1
return lowPints
#the displayInfo function
def displayInfo(averagePints, highPints, lowPints):
print "The average number of pints donated is ", averagePints
print "The highest pints donated is ", highPints
print "The lowest pints donated is ", lowPints
# calls main
main()
最佳答案
函数 getLow()
中有一个无限循环,因为 counter
仅当当前值小于前一个值时才会递增。例如输入值 1, 2, 3, 4, 5, 6, 7
将导致无限循环,但是,如果输入 7, 6, 5, 4, 3,循环将终止, 2, 1
.
函数getHigh()
也有类似的问题,但如果要避免无限循环,值必须升序。请注意,getLow()
或 getHigh()
之一始终会在代码中产生循环。
提示:看看如何使用 Python 的 min()
和 max()
函数。
关于python - 我的显示模块不显示(Python 2.7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23903347/