2016-08-12 111 views
0

我想解決一個問題,但我一直在努力這麼久,並嘗試了這麼多的東西,但我真的是新的蟒蛇,不知道如何得到輸入我之後。蟒蛇雨量計算器

計算器需要採用嵌套循環的格式。首先它應該要求計算降雨的週數。外循環將每週迭代一次。內循環將重複七次,每週一次。內循環的每個迭代都應該要求用戶輸入當天下雨的毫米數。接着計算總降雨量,每週平均值和每日平均值。

我有越來越的多少個星期有和輸入一週的天在節目如迭代的主要麻煩:

Enter the amount of rain (in mm) for Friday of week 1: 5 
Enter the amount of rain (in mm) for Saturday of week 1: 6 
Enter the amount of rain (in mm) for Sunday of week 1: 7 
Enter the amount of rain (in mm) for Monday of week 2: 7 
Enter the amount of rain (in mm) for Tuesday of week 2: 6 

這是類型OUT輸出我想,但到目前爲止,我不知道如何讓它做我想做的事。我想我需要使用字典,但我不知道該怎麼做。這是我的代碼到目前爲止:

ALL_DAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] 
total_rainfall = 0 
total_weeks = 0 
rainfall = {} 

# Get the number of weeks. 
while True: 
    try: 
     total_weeks = int(input("Enter the number of weeks for which rainfall should be calculated: ")) 
    except ValueError: 
     print("Number of weeks must be an integer.") 
     continue 

    if total_weeks < 1: 
     print("Number of weeks must be at least 1") 
     continue 
    else: 
     # age was successfully parsed and we're happy with its value. 
     # we're ready to exit the loop! 
     break 

for total_rainfall in range(total_weeks): 
    for mm in ALL_DAYS: 
     mm = int(input("Enter the amount of rain (in mm) for ", ALL_DAYS, "of week ", range(total_weeks), ": ")) 
     if mm != int(): 
      print("Amount of rain must be an integer") 
     elif mm < 0 : 
      print("Amount of rain must be non-negative") 

    # Calculate totals. 
    total_rainfall =+ mm 
    average_weekly = total_rainfall/total_weeks 
    average_daily = total_rainfall/(total_weeks*7) 
    # Display results. 
    print ("Total rainfall: ", total_rainfall, " mm ") 
    print("Average rainfall per week: ", average_weekly, " mm ") 
    print("Average rainfall per week: ", average_daily, " mm ") 

    if __name__=="__main__": 
     __main__() 

如果你能引導我在正確的方向我會很感激!

+1

你能改正你的縮進嗎? – oldrinb

+0

您可能不應將您的循環變量命名爲稍後更新的內容......例如。 'total_rainfall在範圍內(total_weeks):'...在這裏你循環的是週數,而不是total_rainfall。然後'在ALL_DAYS中爲mm',你循環了幾天,而不是mm。 –

+0

@oldrinb這樣更好嗎? – remar311

回答

0

建議:把問題分解成更小的碎片。最好的方法是使用單獨的功能。

例如,獲取周

def get_weeks(): 
    total_weeks = 0 
    while True: 
     try: 
      total_weeks = int(input("Enter the number of weeks for which rainfall should be calculated: ")) 
      if total_weeks < 1: 
       print("Number of weeks must be at least 1") 
      else: 
       break 
     except ValueError: 
      print("Number of weeks must be an integer.") 

    return total_weeks 

的數目。然後,獲得了一定的星期數和天毫米輸入。 (這裏是你的預期輸出存在的地方)

def get_mm(week_num, day): 
    mm = 0 
    while True: 
     try: 
      mm = int(input("Enter the amount of rain (in mm) for {0} of week {1}: ".format(day, week_num))) 
      if mm < 0: 
       print("Amount of rain must be non-negative") 
      else: 
       break 
     except ValueError: 
      print("Amount of rain must be an integer") 

    return mm 

兩個函數來計算平均值。首先是列表,第二列是列表。

# Accepts one week of rainfall 
def avg_weekly_rainfall(weekly_rainfall): 
    if len(weekly_rainfall) == 0: 
     return 0 
    return sum(weekly_rainfall)/len(weekly_rainfall) 

# Accepts several weeks of rainfall: [[1, 2, 3], [4, 5, 6], ...]  
def avg_total_rainfall(weeks): 
    avgs = [ avg_weekly_rainfall(w) for w in weeks ] 
    return avg_weekly_rainfall(avgs) 

使用這些,你可以建立你的降雨星期到自己的名單。

# Build several weeks of rainfall 
def get_weekly_rainfall(): 
    total_weeks = get_weeks() 
    total_rainfall = [] 

    for week_num in range(total_weeks): 
     weekly_rainfall = [0]*7 
     total_rainfall.append(weekly_rainfall) 

     for i, day in enumerate(ALL_DAYS): 
      weekly_rainfall[i] += get_mm(week_num+1, day) 

    return total_rainfall 

然後,您可以編寫一個接受「主列表」的函數,並打印出一些結果。

# Print the output of weeks of rainfall 
def print_results(total_rainfall): 
    total_weeks = len(total_rainfall) 
    print("Weeks of rainfall", total_rainfall) 

    for week_num in range(total_weeks): 
     avg = avg_weekly_rainfall(total_rainfall[week_num]) 
     print("Average rainfall for week {0}: {1}".format(week_num+1, avg)) 

    print("Total average rainfall:", avg_total_rainfall(total_rainfall)) 

最後,只需兩行來運行完整的腳本。

weekly_rainfall = get_weekly_rainfall() 
print_results(weekly_rainfall) 
+0

謝謝,它有助於打破代碼,而不是一下子想辦法。 – remar311

0

我使用一個列表來存儲每週的平均下落。 和我的循環是:

1.當循環--->本週(用我來算)

2.in while循環:初始化week_sum = 0,則使用循環來問7天降雨。

3.退出for循環,平均降雨量,並附加到列表weekaverage。

4.增加week_sum總降雨量,和i + = 1到下週

weekaverage=[] 
i = 0 #use to count week 

while i<total_weeks: 
    week_sum = 0. 
    print "---------------------------------------------------------------------" 
    for x in ALL_DAYS: 
     string = "Enter the amount of rain (in mm) for %s of week #%i : " %(x,i+1) 
     mm = float(input(string)) 
     week_sum += mm 
    weekaverage.append(weeksum/7.) 
    total_rainfall+=week_sum 
    print "---------------------------------------------------------------------" 
    i+=1 

print "Total rainfall: %.3f" %(total_rainfall) 
print "Day average is %.3f mm" %(total_rainfall/total_weeks/7.) 

a = 0 

for x in weekaverage: 
    print "Average for week %s is %.3f mm" %(a,x) 
    a+=1