我想解決一個問題,但我一直在努力這麼久,並嘗試了這麼多的東西,但我真的是新的蟒蛇,不知道如何得到輸入我之後。蟒蛇雨量計算器
計算器需要採用嵌套循環的格式。首先它應該要求計算降雨的週數。外循環將每週迭代一次。內循環將重複七次,每週一次。內循環的每個迭代都應該要求用戶輸入當天下雨的毫米數。接着計算總降雨量,每週平均值和每日平均值。
我有越來越的多少個星期有和輸入一週的天在節目如迭代的主要麻煩:
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__()
如果你能引導我在正確的方向我會很感激!
你能改正你的縮進嗎? – oldrinb
您可能不應將您的循環變量命名爲稍後更新的內容......例如。 'total_rainfall在範圍內(total_weeks):'...在這裏你循環的是週數,而不是total_rainfall。然後'在ALL_DAYS中爲mm',你循環了幾天,而不是mm。 –
@oldrinb這樣更好嗎? – remar311