How do I called upon variables from previous functions and ensure they are capturing the proper values?

Viewed 37

I am writing a program in Python where the user inputs a time in a 24 - hour format #:## or ##:##, and if it is between the hours of 7-8, output is breakfast, if it is between the hours of 12-13, output is lunch, and if it is between the hours of 18-19, output is dinner; otherwise nothing is output.

As of now, the program is not printing material whatsoever. I am confused if it is the way I am calling the functions or the messy way I organized the variables. I would like to keep the two separate functions of main() and convert(time).

Here is my code so far. Any help is appreciated:

time = str()
hours = str()
minutes = str()

hours_time = float()
minutes_time = float()
minutes_exact = float()

total_time = float()

def convert(time):
    time = str(input("What time is it? "))
    hours, minutes = time.split(':')

    hours_time = float(hours)
    minutes_time = float(minutes) 

    minutes_exact = (minutes_time / 100) 

    return hours_time, minutes_exact


def main():
    convert(time)

    total_time = hours_time + minutes_exact

    if 7.0 <= total_time <= 7.6:
        print("breakfast time")

    elif 12.0 <= total_time <= 12.6:
        print("lunch time")
    
    elif 18.0 <= total_time <= 18.6:
        print("dinner time")

main()
2 Answers

You are defining variables where they are not needed.

You should pass into a function only what it needs and return and accept what it produces.

You meant to write this program:

def convert():
    time = input("What time is it? ")
    hours, minutes = time.split(':')

    hours_time = float(hours)
    minutes_time = float(minutes) 

    minutes_exact = (minutes_time / 100) 

    return hours_time, minutes_exact


def main():
    hours_time, minutes_exact = convert()

    total_time = hours_time + minutes_exact

    if 7.0 <= total_time <= 7.6:
        print("breakfast time")

    elif 12.0 <= total_time <= 12.6:
        print("lunch time")
    
    elif 18.0 <= total_time <= 18.6:
        print("dinner time")

main()

while calling your convert function you are not defining any variable.

if you define the variables hours_time and minutes_exact as the value returned by the convert function, your code will be running propperly.

hours_time,minutes_exact = convert(time)

Example I/O:

What time is it? 07:55
breakfast time

What time is it? 12:51
lunch time

Hope this helps.

Related