我正在尝试利用TeamTreehouse学习订阅&这本《从编程逻辑和设计开始》一书尝试学习编程&蟒蛇请不要开枪打死我,我在重复结构方面有困难!
目标:我正在尝试从外部for循环中的用户那里收集输入。内循环每次外循环迭代计算将迭代12次;获取每个月的降雨量。然后,外环将;显示整个时间段(1或7年等)的月数、总降雨量和每月平均降雨量。
相反,我得到了以下错误。我试着修改我的代码,使计数器变量I成为字符串输入,这样在某种程度上我就可以打印出递增的数字。
我在下面的网址上发现了一个类似的问题,但老实说,这对我来说没有多大意义,因为我不明白。答案中的格式是什么?
TypeError:输入最多需要1个参数,得到3个
错误:
Traceback (most recent call last):
File "python", line 45, in <module>
File "python", line 25, in userInput
TypeError: input expected at most 1 arguments, got 2
代码
#//////MAIN PROGRAM START//////
#//////VARIABLE DECLARATION//////
years=0
timeTraveling=0
months=12
totalMonths=0
rainAverage=0
rainFall=0
#//////VARIABLE DECLARATION//////
#//////USER INPUT FUNCTION//////
def userInput():
rainTotal=0
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=0
monthlyRainFall=int(input("Please provide the rainfall in inches for month number:", i))
rainTotal = rainTotal + monthlyRainFall
rainAverage=rainTotal/months
totalMonths=years*months
#//////testing variable <> value assignment/////
print(rainTotal, 'inches of rain')
#//////testing variable <> value assignment/////
#///////// python code references/////////////
# print('Calculating for a total number of', totalMonths, 'months.')
# print('Monthsttt' + 'Average Rainfall')
# print(rain, 'ttttt', i)
#/////////format references/////////////
print("Average Rainfall per month:", rainTotal/(years*12))
# after the inner loop runs the following should display
# print('number of months
# print('total inches of rainfall
# print('average rainfall per month
#//////CALLING FUNCTION//////
userInput()
#//////CALLING FUNCTION//////
您可能需要制作第25行:
monthlyRainFall=int(input("Please provide the rainfall in inches for month number:"+str(i) ))
如果你只想打印出与i 相关的值
问题就在这里:
monthlyRainFall=int(input("Please provide the rainfall in inches for month number:", i))
您向input()
发送了两个参数:一个是提示用户的字符串,另一个是i
。与print()
不同,input()
只接受一个参数。试试这个:
monthlyRainFall=int(input("Please provide the rainfall in inches for month number: " + str(i)))