在Python3中,我得到了一个在赋值前引用的局部变量'getBottles‘。我真的不知道我做错了什么。赋值前引用是什么意思?
# 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()发布于 2018-10-26 09:06:39
在python中,您必须在使用函数之前定义它们,这意味着在文档中的较低行号。这更接近于您想要的代码:
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()https://stackoverflow.com/questions/52999994
复制相似问题