我有点理解可能发生的事情。显然没有达到这个条件。有点像i = 0到b,其中b等于0或null。 我想要两个日期之间的天数,如9-01-2016和10-31-2016。所以它应该是61天。
# 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 :(得分:1)
您的代码有几个问题。首先是简单的事情:
starting_Month
和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))