2011-03-23 67 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))