私はPythonを学んでいるだけで、 'for range'ループの引数を変数に渡す方法を理解しようとしています。以下のコードでは、 'months'変数を月の名前(Jan、Febなど)にしたいと思います。それから私は、 'sales'変数が 'Janの売上を入力する'というユーザーのプロンプトを出そうとしています。その後、次の繰り返しで次の月に移動します。「Febの売上を入力してください」Python 2.5でネストされたFor Rangeループの引数を渡す
ありがとうございます。
def main():
number_of_years = input('Enter the number of years for which you would like to compile data: ')
total_sales = 0.0
total_months = number_of_years * 12
for years in range(number_of_years):
for months in range(1, 13):
sales = input('Enter sales: ')
total_sales += sales
print ' '
print 'The number of months of data is: ', total_months
print ' '
print 'The total amount of sales is: ', total_sales
print ' '
average = total_sales/total_months # variable to average results
print 'The average monthly sales is: ', average
main()
calendar.month_name [1] == 'January'など、手軽ではあるが手技的には便利なので、手動で行うには+1。 – DSM
'enumerate(months、1)'を提案する。 –
セールス[名前]を直接使用しないのはなぜですか? – Rod