2016-09-21 127 views
0

我有點理解可能發生的事情。情況顯然沒有得到滿足。有點像i = 0到b,其中b等於0或null。 我希望像9-01-2016和10-31-2016這兩個日期之間的天數。所以它應該是61天。我需要幫助才能使for循環工作

# dates are easily constructed and formatted 
#from datetime import datetime, timedelta 
from datetime import datetime 

year = 2016 
left_over_pill_count = input('How many pills did you have left? ') 
new_prescription = input('How many pills did you get? ') 
total_pills = int(left_over_pill_count) + int(new_prescription) 
daily_pill_intake = input('How many pills do you take? ') 
starting_Month = input('Starting Month, Type 1 for January, 2 for February, etc.') 
starting_Day = input('Starting Day; Type 1-31') 
ending_Month = input('Ending Month, Type 1 for January, 2 for February, etc.') 
ending_Day = input('Starting Day; Type 1-31') 


# count number of days until next doctors appointment 
date1 = datetime.date(datetime.strptime((str(year) + "-" + str(starting_Month) + "-" + str(starting_Day)), '%Y-%m-%d')) 
date2 = datetime.date(datetime.strptime((str(year) + "-" + str(ending_Month) + "-" + str(ending_Day)), '%Y-%m-%d')) 

#date_count = (date2 - date1) 
#total_days = date_count 

# fmt = '%Y-%m-%d %H:%M:%S' 
#fmt = '%d' 
#d1 = datetime.strptime(date1, fmt) 
#d2 = datetime.strptime(date2, fmt) 

# print (d2-d1).days * 24 * 60 

for i in range(date1.month, date2.month): 
    if (date1.month == 1): 
     for j in range(date1.day, 31): 
      total_pills = total_pills - int(daily_pill_intake) 
    if (date1.month == 2): 
     for j in range(date1.day, 28): 
      total_pills = total_pills - int(daily_pill_intake) 
    if (date1.month == 3): 
     for j in range(date1.day, 31): 
      total_pills = total_pills - int(daily_pill_intake) 
    if (date1.month == 4): 
     for j in range(date1.day, 30): 
      total_pills = total_pills - int(daily_pill_intake) 
    if (date1.month == 5): 
     for j in range(date1.day, 31): 
      total_pills = total_pills - int(daily_pill_intake) 
    if (date1.month == 6): 
     for j in range(date1.day, 30): 
      total_pills = total_pills - int(daily_pill_intake) 
    if (date1.month == 7): 
     for j in range(date1.day, 31): 
      total_pills = total_pills - int(daily_pill_intake) 
    if (date1.month == 8): 
     for j in range(date1.day, 31): 
      total_pills = total_pills - int(daily_pill_intake) 
    if (date1.month == 9): 
     for j in range(date1.day, 30): 
      total_pills = total_pills - int(daily_pill_intake) 
    if (date1.month == 10): 
     for j in range(date1.day, 31): 
      total_pills = total_pills - int(daily_pill_intake) 
    if (date1.month == 11): 
     for j in range(date1.day, 30): 
      total_pills = total_pills - int(daily_pill_intake) 
    if (date1.month == 12): 
     for j in range(date1.day, 31): 
      total_pills = total_pills - int(daily_pill_intake) 



#for i in range(1, int(date1.day-date2.day)): 
# total_pills = total_pills - int(daily_pill_intake) 
    # print(total_pills) 


print("Taking " + str(daily_pill_intake) + " a day, you should have " + str(total_pills) + " left.") 
+0

告訴他們放入'2016-01-02'這樣的日期,你的生活會有很大改善。 –

+0

另外,你應該考慮[字符串格式化](http://pyformat.info),而不是簡單的連接。 –

回答

1

你的代碼有幾個問題。首先簡單的事情:

  • starting_Month and starting_Day。帽只屬於課程名稱,例如class MyClass:
  • datetime有一個.date()函數只返回日期。
  • 字符串格式是方式比連接更好。比較:

    '{}-{}-{}'.format(year, starting_month, starting_day) 
    

    你有什麼。

  • 已將的答案註釋掉 - print((d1-d2).days * 24 * 60)。只是改變了:print((date1-date2).days)
  • 其實你可以只使用數學這裏:

    pills_left = initial_pill_count - (daily_intake * number_of_days) 
    

把所有這些組合起來,這裏是你應該怎麼寫了這個:

from datetime import datetime 

# Just convert to int when you get the input. Also, only one space 
# before input. 
leftover_pill_count = int(input('How many pills do you have left? ')) 
new_pill_count = int(input('How many pills did you get? ')) 
daily_pill_intake = int(input('How many pills do you take every day? ')) 
# And one space before input! 
start_date = input('Starting date (YYYY-MM-DD): ') 
end_date = input('End date (YYYY-MM-DD): ') 

# Using the same format multiple places? Use a variable! 
# that way you only have to change it once :) 
date_fmt = '%Y-%m-%d' 
start_date = datetime.strptime(start_date, date_fmt) 
end_date = datetime.strptime(end_date, date_fmt) 
# `end-start`, always. Or you could use `abs(start-end)`, 
# but don't do that. 
total_days = (end_date-start_date).days 

initial_pill_count = leftover_pill_count + new_pill_count 

# Here's how the loop *should* look 
loop_pills_left = initial_pill_count 
for i in range(total_days): 
    loop_pills_left -= daily_pill_intake 
    print(loop_pills_left) 

# Or you could approach it mathematically!  
math_end_count = initial_pill_count - (daily_pill_intake * total_days) 

# Let's just make sure that both approaches get us the same answer, though 
assert math_end_count == loop_pills_left, 'If this fails, we got our math wrong' 
pills_left = loop_pills_left 

# String formatting is *much* nicer than concatenation. 
print('Taking {} pills per day you should have {} pills left.' 
     .format(daily_pill_intake, pills_left)) 
+0

謝謝你的幫助!你的代碼看起來好上百萬倍。 –

+0

如果這樣可以解決您的問題,您應該將它標記爲已接受,左邊的複選標記'<----' –