2016-09-25 9 views
0

年の入力値が2の場合、最初の12ヶ月間は1年、次の12ヶ月間は2年目にプログラムを表示するにはどうすればよいですか?ネストされたループ計算の出力が正しくないが、プログラムが実行される

また、計算がうまくいかないこともわかりません。私の所望の出力によると、総雨量出力は37でなければなりませんが、私はprint文の前に39

Added picture of current output. Total should be at 37 not 39. I also need the second batch of 12 months to say year 2

#the following are the values for input: 
#year 1 month 1 THROUGH year 1 month 11 = 1 
#year 1 month 12 THROUGH year 2 month 12 = 2 

def main(): 
    #desired year = 2 
    years = int(input("Enter the number of years you want the rainfall calculator to determine: ")) 
    calcRainFall(years) 

def calcRainFall(yearsF): 
    months = 12 
    grandTotal = 0.0 

    for years_rain in range(yearsF): 
     total= 0.0 
     for month in range(months): 
      print('Enter the number of inches of rainfall for year 1 month', month + 1, end='') 
      rain = int(input(': ')) 
      total += rain 
     grandTotal += total 
    #This is not giving me the total I need. output should be 37. 
    #rainTotal = rain + grandTotal 
    #print("The total amount of inches of rainfall for 2 year(s), is", rainTotal) 
    print("The total amount of inches of rainfall for 2 year(s), is", grandTotal) 
main() 
+0

あなたは何のための 'years_rain'を使用していません。 'year 1'の代わりにそれを印刷してみてください。 –

+0

私は構文エラーのために私をさせてくれません。 – Alina

+0

構文エラーとは何ですか?コードは何ですか?それを質問に入れてください。 –

答えて

2

コードを少し短くしました。あなたのコードのための

def main(): 
    years = int(input("Enter the number of years you want the rainfall calculator to determine: ")) 
    calcRainFall(years) 

def calcRainFall(yearsF): 
    months = 12 * yearsF # total number of months 
    grandTotal = 0.0  # inches of rain 

    for month in range(months): 
     # int(month/12): rounds down to the nearest integer. Add 1 to start from year 1, not year 0. 
     # month % 12: finds the remainder when divided by 12. Add 1 to start from month 1, not month 0. 
     print('Enter the number of inches of rainfall for year', int(month/12) + 1, 'month', month % 12 + 1, end='') 
     rain = int(input(': ')) 
     grandTotal += rain 
    print("The total amount of inches of rainfall for", yearsF, "year(s), is", grandTotal) 

main() 
+0

これは機能しましたが、どのようにして2年目に行くことができたのか少し失われていることをさらに説明できますか? – Alina

+0

最初の12ヶ月(月= 0..11)、月/ 12 = 0は何かを指しています。 次の12ヶ月間(月= 12..23)、月/ 12 = 1点を何か指します。 int()を使用すると、小数点以下のものは無視されます。それは基本的に0から始まる年数を与えます。したがって、私たちは+1します。 – Ahmad

3

を取得しています、あなたはrainTotalのために再び雨値を追加する必要はありません。これは、grandTotalが年間雨の原因となるためです。そして、すでに2年に2度追加されています。それでは、あなたは基本的に(この場合は2)を2回雨の最後の値を追加してやっている これをあなたのprint文を作成し、削除rainTotal -

print("The total amount of inches of rainfall for 2 year(s), is", grandTotal) 
+0

それはトリックでした。 2年目の出力を年2とし、年を1としないようにすることができますか? – Alina

+0

これをあなたのprintステートメントの中で使ってください。 'print( '年の降雨量を入力してください+ + str(year_rain)+'月 '、str(月+ 1)) ' – Zeokav

+0

これは年を0から始める – Alina

0
rainTotal = rain + grandTotal 

には、次の実行している:2 + 37あなたは既に最後の雨入力= 2との合計を持っているかgrandTotalは= 37(各年の総入力)grandTotalが必要とされていない+そうrainTotal =雨なので

+0

最新の編集でそれを変更できました。すべての成果が1年目ではなく、次の12ヶ月目を2年目にする方法を決定できますか? – Alina

+0

あなたが必要とする:年1 = 12ヶ月と合計を計算し、2 = 12ヶ月と合計を計算する? – sb0709

+0

私が入力した値なので、私の出力は2年間皿洗いされます。それらの月のうち12ヶ月が「年1」とラベル付けされ、他の12個が「年2」とラベル付けされるようにするにはどうすればよいですか? "降水量の年数を入力してください(これは月の後半に2を置く必要がある領域です)" – Alina

0

注:

先に述べたように、rainTotalは不要ですうまくいけば、これは完全で正しいプログラムです。

あなたは試すことができます:

print 'Enter the number of inches of rainfall for year %d month %d' % (years_rain, month), end='') 

これはあなたのループを実行するためとしてyears_rainの値と月の値を有する第二%dの最初の%dを記入します。

以下に示すように、このトリックは、最終的な印刷ラインでも使用することができます:

print("The total amount of inches of rainfall for %d year(s) % yearsF, is", grandTotal) 
関連する問題