2011-03-23 69 views
0

作业问题的类,包括调用一些for循环,这里的硬件问题:在类型错误嵌套的循环

平均温度

编写使用嵌套的循环来收集数据并计算平均温度的程序在几个月内。该计划应该首先要求的月数。外循环将每月迭代一次。内循环将迭代四次,每月一次。内循环的每次迭代都会询问用户该周的平均温度。所有迭代后,程序应显示平均温度每一个月,整个时段(所有月份)

这里是我做过什么:

def avg_temp(): 
temp_sum=0 
num= input('Please enter the number of months: ') 
for i in range(1,num+1): 
    for y in range(1,5): 
     num1= input('Please enter the average temperature for week ',y,'in month ',i,': ') 
     temp_sum+=num1 
    avg_temp_month==(temp_sum/4) 
    print 'The average temperature for month ',i,'is: ',avg_temp_month 
avg_temp_period==(avg_temp_month/num) 
print 'The average temperature for all ',num,' months is: ',avg_temp_period 
avg_temp() 

当我输入的输入值,在这种情况下,5,这是我收到的错误:

Please enter the number of months: 5 
Traceback (most recent call last): 
File "C:/Users/Jonathan Cohen/Desktop/School/CISC 106 Spring/lab4.py", line 22, in  
avg_temp() File "C:/Users/Jonathan Cohen/Desktop/School/CISC 106 Spring/lab4.py", line 
15, in avg_temp num1= input('Please enter the average temperature for week ',y,'in  
month ',i,': ') TypeError: [raw_]input expected at most 1 arguments, got 5 

任何帮助,非常感谢!

+0

NUM1 =输入这个唯一功能接受1个参数,而不是5 – 2011-03-23 05:16:29

+0

考虑编写多个输入调用foreach输入变量 – 2011-03-23 05:17:39

回答

1

读取错误:你是给输入5个参数。

的毗连字符串用​​+运营商,而不是( '在一个月' 请输入周平均温度,γ,我, ':')逗号

1

num1 = input('Please enter the average temperature for week %s in month %s: '%(y,i))