简体   繁体   English

无法使创建的 python function 返回一个值,然后我可以在另一个计算中使用该值

[英]Can't make created python function return a value which I can then use in another calculation

I have defined a function that asks what time you start on a work day and a function that asks when you finish.我定义了一个 function 询问您在工作日什么时候开始,以及一个 function 询问您何时完成。 I'm now trying to define a function that calculates the time difference between them and a problem I am running into is actually using the result of calling the start/finish functions.我现在正在尝试定义一个 function 来计算它们之间的时间差,而我遇到的问题实际上是使用调用开始/完成函数的结果。

I am having a hard time understanding how to make the function return the value I want ie when I call the function and add the Monday parameter, thus asking what time work was started specifically on Monday.我很难理解如何让 function 返回我想要的值,即当我调用 function 并添加 Monday 参数时,从而询问具体在星期一开始工作的时间。 I'm not sure how to get a result of something like '10:00' so I am able to use that resulting return in another function.我不确定如何获得类似“10:00”的结果,因此我可以在另一个 function 中使用该结果。

Also the further I get into writing this code, the more I get confused about how to differentiate the days.此外,我越深入编写此代码,我就越对如何区分日子感到困惑。 As in, I have created a function which asks what time you start and then I combined that with another function which basically cycles through the days of the week.例如,我创建了一个 function,它询问你什么时候开始,然后我将它与另一个 function 结合起来,它基本上在一周中的几天循环。 I'm realising that I don't know how I would separate the ask_start_time function for a Monday from the ask_start_time function for a Thursday.我意识到我不知道如何将星期一的ask_start_time function 与星期四的ask_start_time function 分开。

The error I get is:我得到的错误是:

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

Code:代码:

    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))

The problem is that ask_start_time and ask_finish_time returns nothing, or None.问题是ask_start_timeask_finish_time什么都不返回,或者没有返回。 Return the parsed times from those functions and your code will work.从这些函数返回解析的时间,您的代码将起作用。

def ask_start/end_time(...):
    ...
    return start/finish

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在 python 中有一个方法/函数,我试图在其中获取一个返回值,然后我可以将其用作另一个函数中的变量 - 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 如何在 Python 3 中获取一个函数以返回我可以在另一个函数中使用的值? - How can I get a function in Python 3 to return a value that I can use in another function? 当 python 没有返回时,如何访问另一个 function 中的值? - How can I access value in another function when there is no return at python? 函数可以返回另一个 function 用于 Python - Functions can return another function use in Python 为什么我不能在 python 的 lambda 函数中使用“return”? - Why can't I use “return” in lambda function in python? 如何在python的另一个应用程序中使用函数返回的值? - How can I use a value returned by a function in another application in python? 无法从 Python 中的函数返回值 - Can't return value from function in Python 如何返回由函数创建的套接字并将其传递给另一个? - How can I return socket created by function and pass it to another? 我无法在定义函数中返回值 - I can't return a value in define function 如何使用 docplex 从第一次计算中获取解决方案值并将其用于 python 中的另一次计算 - How can I take the solution values from first calculation and use it in another calculation in python using docplex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM