首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的显示模块不显示(Python 2.7)

我的显示模块不显示(Python 2.7)
EN

Stack Overflow用户
提问于 2014-05-28 05:21:37
回答 1查看 110关注 0票数 1

这是做作业用的。这所特殊的学校提供0的帮助,而教授则没有多大帮助。我只是在寻求关于为什么这段代码不起作用的指导。我必须使用Python2.7。当我运行程序时,它要求我输入适当数量的品脱,但之后什么也不做。

代码语言:javascript
复制
# 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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-28 05:36:06

函数getLow()中有一个无限循环,因为只有当当前值小于前一个值时,counter才会增加。例如,输入值1, 2, 3, 4, 5, 6, 7将导致无限循环,但是,如果输入7, 6, 5, 4, 3, 2, 1,则循环将终止。

函数getHigh()也有类似的问题,但是如果要避免无限循环,则这些值必须是上升的。请注意,getLow()getHigh()之一将始终在代码中生成一个循环。

提示:看看如何使用Python的min()max()函数。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23903347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档