[英]I have a method / function in python in which i'm trying to get a return value which i can then use as a variable in another function
[英]Can't make created python function return a value which I can then use in another calculation
我定義了一個 function 詢問您在工作日什么時候開始,以及一個 function 詢問您何時完成。 我現在正在嘗試定義一個 function 來計算它們之間的時間差,而我遇到的問題實際上是使用調用開始/完成函數的結果。
我很難理解如何讓 function 返回我想要的值,即當我調用 function 並添加 Monday 參數時,從而詢問具體在星期一開始工作的時間。 我不確定如何獲得類似“10:00”的結果,因此我可以在另一個 function 中使用該結果。
此外,我越深入編寫此代碼,我就越對如何區分日子感到困惑。 例如,我創建了一個 function,它詢問你什么時候開始,然后我將它與另一個 function 結合起來,它基本上在一周中的幾天循環。 我意識到我不知道如何將星期一的ask_start_time
function 與星期四的ask_start_time
function 分開。
我得到的錯誤是:
What time did you start on Monday?10:00
Traceback (most recent call last):
File "C:/Users/MM/PycharmProjects/pythonProject2/test.py", line 49, in <module>
start_datetime = datetime.datetime.strptime(mon, '%H:%M')
TypeError: strptime() argument 1 must be str, not None
代碼:
for a in range(attempts):
start = input(f'What time did you start on {day_name}?')
validation = re.match("^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$", start)
if validation:
break
print('Please use a HH:MM format only.')
else:
print('25 wrong attempts and you still don\'t understand that it\'s HH:MM?!')
def ask_finish_time(day_name, attempts=25):
for a in range(attempts):
finish = input(f'What time did you finish on {day_name}?')
validation = re.match("^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$", finish)
if validation:
break
print('Please use a HH:MM format only.')
def days():
work_days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
start_times = {day: ask_start_time for day in work_days}
print(start_times)
def time_diff(a, b):
return b - a
ask_start_time('Monday')
start_datetime = datetime.datetime.strptime(ask_start_time, '%H:%M')
ask_finish_time('Monday')
finish_datetime = datetime.datetime.strptime(ask_finish_time, '%H:%M')
print(time_diff(start_datetime, finish_datetime))
def ask_start_time(day_name, attempts=25):
for a in range(attempts):
start = input(f'What time did you start on {day_name}?')
validation = re.match("^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$", start)
if validation:
return start
print('Please use a HH:MM format only.')
else:
print('25 wrong attempts and you still don\'t understand that it\'s HH:MM?!')
def ask_finish_time(day_name, attempts=25):
for a in range(attempts):
finish = input(f'What time did you finish on {day_name}?')
validation = re.match("^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$", finish)
if validation:
return finish
print('Please use a HH:MM format only.')
def days():
work_days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
start_times = {day: ask_start_time for day in work_days}
print(start_times)
def time_diff(a, b):
return b - a
start = ask_start_time('Monday')
start_datetime = datetime.datetime.strptime(start, '%H:%M')
finish = ask_finish_time('Monday')
finish_datetime = datetime.datetime.strptime(finish, '%H:%M')
print(time_diff(start_datetime, finish_datetime))
問題是ask_start_time
和ask_finish_time
什么都不返回,或者沒有返回。 從這些函數返回解析的時間,您的代碼將起作用。
def ask_start/end_time(...):
...
return start/finish
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.