2016-09-21 34 views
0

我不明白爲什麼這段代碼不起作用。我想創建一些代碼來幫助我確切地知道需要將多少藥丸恢復到疼痛管理。如果你沒有把適量的錢拿回來,那麼你會被踢出痛苦管理。所以我只是想創建一個能夠幫助我的腳本,所以我不會回頭。如何創建像疼痛管理設施使用的處方藥計數?

正如任何人都可以說的。我對Python沒有任何經驗。我只是安裝它,並嘗試使用文檔來幫助完成我認爲是一個簡單的腳本。

Traceback (most recent call last): 
    File "C:\Users\howell\AppData\Local\Programs\Python\Python35-32\Scripts\pill_count.py", line 17, in <module> 
    date1 = datetime.date(datetime.strptime((str(year) + "-" + str(starting_Month) + "-" + str(starting_Month) + "-" + str(starting_Day)), '%Y-%m-%d')) 
    File "C:\Users\howell\AppData\Local\Programs\Python\Python35-32\lib\_strptime.py", line 510, in _strptime_datetime 
    tt, fraction = _strptime(data_string, format) 
    File "C:\Users\howell\AppData\Local\Programs\Python\Python35-32\lib\_strptime.py", line 346, in _strptime 
    data_string[found.end():]) 
ValueError: unconverted data remains: -1 
How many pills did you have left? 12 
How many pills did you get? 90 
How many pills do you take? 6 
Starting Month, Type 1 for January, 2 for February, etc.9 
Starting Day; Type 1-311 
Ending Month, Type 1 for January, 2 for February, etc.10 
Starting Day; Type 1-3131 
Taking 6 a day, you should have 102 left. 

# 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(1, (date1-date2).days): 
    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.") 
+1

「無法正常工作」並不能真正幫助追蹤問題,您應該編輯問題以包含錯誤。但我想'total_pills = int(left_over_pill_count + new_prescription)'給你一個意想不到的輸出。在添加它們之前,需要分別在兩個輸入上調用'int' – roganjosh

回答

0

在這一行:

date1 = datetime.date(datetime.strptime((str(year) + "-" + str(starting_Month) + "-" + str(starting_Month) + "-" + str(starting_Day)), '%Y-%m-%d')) 

你告訴datetime.strptime解析形式的「年 - 月 - 日」的字符串,但你給它的字符串「年,月,日」形式;你包括這個月兩次!同樣的問題也適用於下一行。

+0

非常感謝您的幫助。我的眼睛沒有收拾任何東西。我做出了改變,並將其付諸實施。那麼,它不會拋出異常,但它不會產生正確的輸出。它產生一個負數。 –

+0

for循環根本沒有運行。它只是返回total_pill計數而不減去每日服用的藥片數量。 –

+0

@KevinHowell:這似乎應該在單獨的問題中提出。 – jwodder