开发者

Type Error in nested for loops

Homework problem for class that involves invoking several for loops, here's the hw problem:

Average Temperature

Write a program that uses nested loops to collect data and calculate the average temperature over a period of months. The program should first ask for the number of months. The outer loop will iterate once for each month. The inner loop wi开发者_开发知识库ll iterate FOUR times, once for each week in a month. Each iteration of the inner loop will ask the user for the average temperature of that week. After all iterations, the program should display the average temperature for each month, and for the entire period (for all the months)

here is what I did:

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()

when I type an input value, in this case 5, this is the error I receive:

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

any help is much appreciated!


Read the error: you are giving input 5 arguments.

Concat strings with + operator, not commas


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


As was suggested in the comments, look at other ways of joining strings.

http://skymind.com/~ocrow/python_string/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜