Skip to content
Advertisement

Python function repeating itself after if statement satisfied

I am a beginner python user and I am stuck with a time-calculator program I am trying to create as part of an online certification. The program will calculate in an AM/PM format the time it is added from the initial time and the correct weekday. I have been having problems with this part as for reasons unknown to me the functions restart after having found the new weekday, assigning the integer “2” from the variable weekday and then breaking.

Here is the code snippet:

day_names = [
    "monday",
    "tuesday",
    "wednesday",
    "thursday",
    "friday",
    "saturday",
    "sunday",
]

def weekday_calculator( weekday, day_count, new_hour, new_minute,):  # this function calculates the right weekday for the new time

> print(f"starting weekday:{weekday}")
> weekday = weekday.lower()
> starting_day_index = day_names.index(weekday)
> print(f"This is the starting day of the week's index: {starting_day_index}")
> print(f"This is the day count {day_count}")

> weekday_calculate = starting_day_index + day_count

> if weekday_calculate <= 6:
    >> new_weekday = day_names[weekday_calculate]  # to be fixed
    >> print(f"This is the new weekday {new_weekday}")
    >> result_printer(new_hour, new_minute, new_am_pm, day_count, new_weekday)

> elif weekday_calculate > 6:
    >> print("let's adjust the weekday")
    >> adjust_weekday(define_weekday)

weekday_calculator(weekday = "tuesday", daycount = 1) #this is only the data relevant to this snippet

This is the expected output:

Let's calculate the weekday
starting weekday:tuesday
This is the starting day of the week's index: 1
This is the day count 1
This is the new weekday Wednesday
(proceeds to the next function)

This is what has been happening

Let's calculate the weekday
starting weekday:tuesday
This is the starting day of the week's index: 1
This is the day count 1
This is the new weekday wednesday
tuesday
starting weekday:2
Traceback (most recent call last) line 52, in weekday_calculator
    weekday = weekday.lower()
AttributeError: 'int' object has no attribute 'lower' # of course that is because you cannot change an integer to lower

Does anyone have an idea on how to fix this problem? I have no idea where the value “2” for weekday is coming from, and neither why the function is repeating itself instead of directly jump to the next one once at the end of the if statement. I have tried to change the structure of the function and the variable names so that the program does not make confusion between weekday and new weekday, but to no avail.

As you rightly requested, I have edited the post and added the rest of the code:

day_names = [
    "monday",
    "tuesday",
    "wednesday",
    "thursday",
    "friday",
    "saturday",
    "sunday",
]
timeday_am = ["PM", "AM"] * 100
timeday_pm = ["AM", "PM"] * 100

weekday = 0


def result_printer(new_hour, new_minute, new_am_pm, day_count, weekday):
    new_time = [new_hour, new_minute]
    for number in new_time:
        if number < 10:
            return f"0{number}"

    if day_count != 0:
        if day_count == 1:
            day = "(next day)"

        else:
            day = f"({day_count} days later)"

    print(f"{new_time[0]}:{new_time[1]} {new_am_pm}, {weekday} {day}")


def adjust_weekday(
    define_weekday,
):  # this is to adjust the weekday index if it is more than 6
    adjusted_weekday = day_names[define_weekday % len(day_names)]
    print((adjusted_weekday))


def weekday_calculator(
    weekday, day_count, new_hour, new_minute, new_am_pm
):  # this function calculates the right weekday for the new time

    print(f"starting weekday:{weekday}")
    weekday = weekday.lower()
    starting_day_index = day_names.index(weekday)
    print(f"This is the starting day of the week's index: {starting_day_index}")
    print(f"This is the day count {day_count}")

    weekday_calculate = starting_day_index + day_count

    if weekday_calculate <= 6:
        new_weekday = day_names[weekday_calculate]  # to be fixed
        print(f"This is the new weekday {new_weekday}")
        result_printer(new_hour, new_minute, new_am_pm, day_count, new_weekday)

    elif weekday_calculate > 6:
        print("let's adjust the weekday")
        adjust_weekday(define_weekday)


def day_calculator(
    new_hour, new_minute, new_am_pm, am_pm, weekday, day_count
):  # this function calculates the right AM PM of the new hour, and the number of days between times (if applicable)

    day_count = day_count

    if new_am_pm == "AM":
        new_am_pm = timeday_am[am_pm]
        print(f"This is the new time of the day list {new_am_pm}")

        day_new = timeday_am[:am_pm]
        print(f"this is the new day {day_new}")

        day_count = day_new.count(
            "AM"
        )  # this is to count how many days have passed from the starting day
        print(f"this is the day count {day_count}")

    elif new_am_pm == "PM":
        new_am_pm_day = timeday_pm[am_pm]
        print(f"This is the new time of the day {new_am_pm}")

        day_new = timeday_pm[:am_pm]
        print(f"this is how it is calculated {day_new}")

        day_count = day_new.count("AM")
        print(f"this is the day count {day_count}")

    if weekday is not None:
        print(weekday)
        print("Let's calculate the weekday")
        weekday_calculator(weekday, day_count, new_hour, new_minute, new_am_pm)

    result_printer(new_hour, new_minute, new_am_pm, day_count, weekday)


def time_calculator(init_time: str, add_time: str, weekday: str):

    day_count = 0

    new_am_pm = init_time.split(" ")[1]

    init_hour = int(init_time.split(":")[0])
    init_minute = init_time.split(":")[1]
    init_minute = int(
        init_minute.split(" ")[0]
    )  # this is to avoid to include AM/PM in the string #this results in problem when python cannot convert string to integer because of formatting ex 00:

    add_hour = int(add_time.split(":")[0])
    add_minute = int(add_time.split(":")[1])

    print(
        f"1. This is the hour to be added: {init_hour} and this is the  minute: {init_minute}"
    )  # @ control string

    new_minute = init_minute + add_minute
    new_hour = init_hour + add_hour

    if new_minute >= 60:
        new_minute -= 60
        new_hour = new_hour + 1

    # calculate am or pm
    am_pm = (
        new_hour // 12
    )  # this starts the process to calculate the right time of the day and day of the week, floor division rounds the number down
    print(f"This is {am_pm} am pm coefficent")  # @control string
    print(type(am_pm))

    # adapt new hour to hour format 0-12

    if new_hour > 12:
        new_hour = new_hour - (am_pm * 12)

    print(
        f"This is the new hour: {new_hour} and this is the new minute: {new_minute}"
    )  # @ control string

    if am_pm < 1:
        new_am_pm = new_am_pm
    else:
        day_calculator(new_hour, new_minute, new_am_pm, am_pm, weekday, day_count)

    if weekday is not None:
        weekday_calculator(new_hour, new_minute, new_am_pm, weekday, day_count)

    result_printer(new_hour, new_minute, new_am_pm, day_count, weekday)


time_calculator("3:10 PM", "23:20", "tuesday")

Advertisement

Answer

The problem is in your time_calculator function. In the last few lines you first call day_calculator(new_hour, new_minute, new_am_pm, am_pm, weekday, day_count) which in turn calls weekday_calculator (this is fine). However, in the next two lines you also call weekday_calculator with completely wrong arguments.

Your function defines the arguments as weekday, day_count, new_hour, new_minute, new_am_pm.

But looking at the call to the function, you pass new_hour, new_minute, new_am_pm, weekday, day_count.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement