简体   繁体   中英

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

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

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

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. Return the parsed times from those functions and your code will work.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM