python - 我的显示模块不显示(Python 2.7)

标签 python

这是做作业的。这所特殊的学校提供​​ 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/

相关文章:

python - 如何迭代由 getter 或 @property 定义的对象的属性?

python - 很好奇解释器在这里做什么

python - 尝试打印时实例对象被删除

python - 在 python web 服务器上执行数学用户代码,最简单的安全方法是什么?

python - 如何在特定时间间隔连续触发 Action ?敌人在pygame中发射恒定光束而不是子弹

python - 两个列表的所有可能替换?

java - 请讨论什么是 portlet 以及为什么要使用 portlet

python - Django - 具有多个选择的下拉表单

python - 何时将子应用拆分为新应用

Python\0 在字符串中后跟数字的行为不一致