我只是學習Python和我試圖找出如何傳遞參數在「爲範圍」循環的變量範圍循環。在下面的代碼中,我希望'月份'變量是月份的名稱(1月,2月等)。然後,我試圖讓'sales'變量的用戶提示爲'輸入Jan的銷售額'。然後下一個迭代,移動到下一個月份 - 「輸入銷售二月」傳遞參數對於在Python 2.5
謝謝你的任何建議。
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] ==「一月」等,這有時是方便的,但對於做手工不教學法上是有用所以+1。 – DSM 2011-02-05 06:41:46