2016-09-21 3 views
0

おそらく起きていることを理解しています。条件は明らかに満たされていません。 i = 0〜bのようなもので、bは0またはnullに等しい。 9-01-2016と10-31-2016のように2つの日付の間の日数が必要です。だから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_Monthstarting_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

これで問題が解決した場合は、左側の '<----' –

関連する問題