无法在内部和外部 for 循环之间传递变量中的值 python 3.3
Inability to pass values in variable between inner and outer for loops python 3.3
我正在尝试使用 TeamTreehouse 学习订阅和这本从编程逻辑和设计开始的书来尝试学习编程和 python。请不要开枪打死我,我在重复结构方面遇到了困难!
目标:我试图在外部 for 循环中收集用户的输入。每次外循环迭代计算,内循环将迭代12次;获取每个月的降雨量。然后外循环;显示整个时间段(1 年或 7 年等)的月数、总降雨量英寸和每月平均降雨量。
我正在阅读有关按引用或按值传递值的内容,发现 python 具有可变和不可变数据类型(其中 int 是不可变数据类型),因此我不能简单地在两者之间传递数据for 循环从我的理解。那么我该如何让它发挥作用呢?虽然我不明白如何从列表中得出平均值,但我已经向我建议了一个列表,因为坦率地说,到目前为止,teamTreehouse 或我的书的第 4 章都没有涉及它。
http://en.wikibooks.org/wiki/Python_Programming/Data_Types
错误:无法将数据从内部嵌套循环变量 rainTotal 传输到外部循环 rainTotal。
代码:
#//////MAIN PROGRAM START//////
#//////VARIABLE DECLARATION//////
totalMonths=0
rainAverage=0
rainFall=0
rainTotal=0
#//////VARIABLE DECLARATION//////
#//////USER INPUT FUNCTION//////
def userInput():
years=0
months=12
#////don't understand how to function properly
# monthly_rain = []
#////don't understand how to function properly
print('This program will calculate the average rainfall over a period of years.')
years=int(input("Please provide the number of years to calculate rainfall for."))
for i in range(1, years + 1):
#////////////////testing variable values correct////////////////
#Placeholder
#////////////////testing variable values correct////////////////
#//////USER INPUT FUNCTION//////
for i in range(1, months + 1):
rainTotal=int()
monthlyRainFall=int(input("Please provide the rainfall in inches for month number " + str(i) + str(": ")))
#////don't understand how to function properly
# monthly_rain.append(monthlyRainFall)
#////don't understand how to function properly
rainTotal = rainTotal + monthlyRainFall
rainAverage=rainTotal/months
#//////testing variable <> value assignment/////
#///////// python code references/////////////
# print('Calculating for a total number of', totalMonths, 'months.')
# print('Months\t\t\t' + 'Average Rainfall')
# print(rain, '\t\t\t\t\t', i)
#/////////format references/////////////
print("There was a total of ", (years*months), "months calculated.")
print("The accumulative total of rainfall was ", rainTotal, " inches!")
print("Average Rainfall per month:", rainTotal/(years*months))
# after the inner loop runs the following should display
#//////CALLING FUNCTION//////
userInput()
#//////CALLING FUNCTION//////
如前所述 - 请详细说明您收到的错误。但是通过查看您的代码,在进入内部循环之前尝试定义 rainTotal 。即:
for i in range(1, years + 1):
rainTotal=int() #here
for i in range(1, months + 1):
我正在尝试使用 TeamTreehouse 学习订阅和这本从编程逻辑和设计开始的书来尝试学习编程和 python。请不要开枪打死我,我在重复结构方面遇到了困难!
目标:我试图在外部 for 循环中收集用户的输入。每次外循环迭代计算,内循环将迭代12次;获取每个月的降雨量。然后外循环;显示整个时间段(1 年或 7 年等)的月数、总降雨量英寸和每月平均降雨量。
我正在阅读有关按引用或按值传递值的内容,发现 python 具有可变和不可变数据类型(其中 int 是不可变数据类型),因此我不能简单地在两者之间传递数据for 循环从我的理解。那么我该如何让它发挥作用呢?虽然我不明白如何从列表中得出平均值,但我已经向我建议了一个列表,因为坦率地说,到目前为止,teamTreehouse 或我的书的第 4 章都没有涉及它。 http://en.wikibooks.org/wiki/Python_Programming/Data_Types
错误:无法将数据从内部嵌套循环变量 rainTotal 传输到外部循环 rainTotal。
代码:
#//////MAIN PROGRAM START//////
#//////VARIABLE DECLARATION//////
totalMonths=0
rainAverage=0
rainFall=0
rainTotal=0
#//////VARIABLE DECLARATION//////
#//////USER INPUT FUNCTION//////
def userInput():
years=0
months=12
#////don't understand how to function properly
# monthly_rain = []
#////don't understand how to function properly
print('This program will calculate the average rainfall over a period of years.')
years=int(input("Please provide the number of years to calculate rainfall for."))
for i in range(1, years + 1):
#////////////////testing variable values correct////////////////
#Placeholder
#////////////////testing variable values correct////////////////
#//////USER INPUT FUNCTION//////
for i in range(1, months + 1):
rainTotal=int()
monthlyRainFall=int(input("Please provide the rainfall in inches for month number " + str(i) + str(": ")))
#////don't understand how to function properly
# monthly_rain.append(monthlyRainFall)
#////don't understand how to function properly
rainTotal = rainTotal + monthlyRainFall
rainAverage=rainTotal/months
#//////testing variable <> value assignment/////
#///////// python code references/////////////
# print('Calculating for a total number of', totalMonths, 'months.')
# print('Months\t\t\t' + 'Average Rainfall')
# print(rain, '\t\t\t\t\t', i)
#/////////format references/////////////
print("There was a total of ", (years*months), "months calculated.")
print("The accumulative total of rainfall was ", rainTotal, " inches!")
print("Average Rainfall per month:", rainTotal/(years*months))
# after the inner loop runs the following should display
#//////CALLING FUNCTION//////
userInput()
#//////CALLING FUNCTION//////
如前所述 - 请详细说明您收到的错误。但是通过查看您的代码,在进入内部循环之前尝试定义 rainTotal 。即:
for i in range(1, years + 1):
rainTotal=int() #here
for i in range(1, months + 1):