首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnboundLocalError:赋值前引用的局部变量“”getBottles“”

UnboundLocalError:赋值前引用的局部变量“”getBottles“”
EN

Stack Overflow用户
提问于 2018-10-26 08:58:40
回答 1查看 315关注 0票数 0

在Python3中,我得到了一个在赋值前引用的局部变量'getBottles‘。我真的不知道我做错了什么。赋值前引用是什么意思?

代码语言:javascript
复制
# Lab 5-4 The Bottle Return Program

#the main function
def main():
    endProgram = 'no'

    while endProgram == 'no':
        totalBottles = getBottles()
        totalPayout = calPayout (totalBottles)
        printInfo (totalBottles, totalPayout)

        #this function will get the number of bottles returned
        def getBottles():
            totalBottles = 0
            todayBottles = 0
            counter = 1
            while counter <= 7:
                todayBottles = input('Enter number of bottles for today: ')
                totalBottles = totalBottles + todayBottles
                counter = + 1
                return totalBottles

        #this function calculates the pay out amount   
        def calcPayout(totalBottles):
            totalPayout = 0
            totalPayout = totalBottles * 0.10
            return totalPayout

        #this funciton displays the results
        def printInfo (totalBottles, totalPayout):
            print('The total number of bottles collected is ', totalBottles)
            print('The total paid out is $', totalPayout)


    endProgram = input('Do you want to end the program? (Enter yes or no): ')

#calls main
main()
EN

回答 1

Stack Overflow用户

发布于 2018-10-26 09:06:39

在python中,您必须在使用函数之前定义它们,这意味着在文档中的较低行号。这更接近于您想要的代码:

代码语言:javascript
复制
def getBottles():
    totalBottles = 0
    todayBottles = 0
    counter = 1
    while counter <= 7:
        todayBottles = input('Enter number of bottles for today: ')
        totalBottles = totalBottles + todayBottles
        counter = + 1
        return totalBottles



#this function calculates the pay out amount   
def calcPayout(totalBottles):
    totalPayout = 0
    totalPayout = totalBottles * 0.10
    return totalPayout

#this funciton displays the results
def printInfo (totalBottles, totalPayout):
    print('The total number of bottles collected is ', totalBottles)
    print('The total paid out is $', totalPayout)


def main():
    endProgram = 'no'

    while endProgram == 'no':
        totalBottles = getBottles()
        totalPayout = calPayout (totalBottles)
        printInfo (totalBottles, totalPayout)
        endProgram = input('Do you want to end the program? (Enter yes or no): ')

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

https://stackoverflow.com/questions/52999994

复制
相关文章

相似问题

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