Using Dictionaries in Python

I have updated the program that I have been writing while following the video I have been watching. I am still having to do some of my own tinkering, as my code is structured slightly different than the one in the video, but works mostly the same, and has a little more entry validation in it. I am pretty sure it could use some more cleaning up, as evolving code usually does, but that can wait until later.

def days_to_units(number_of_days, conversion_unit):
    if conversion_unit == "hours":
        calculation_to_units = 24
    elif conversion_unit == "minutes":
        calculation_to_units = 24*60
    else:
        return("Invalid conversion unit...")
        
    if number_of_days > 0: 
        return f"{number_of_days} days are {number_of_days * calculation_to_units} {conversion_unit}."
    else:
        return "You did not enter a positive integer..."

def validate_and_execute():
    try:
        print(days_to_units(int(days_and_unit_dictionary["days"]), str(days_and_unit_dictionary["unit"])))
    except ValueError:
        print("Please correct your input...")

user_input = 0
while user_input != "quit":
    user_input = input("""Enter the number of days and conversion to unit (10:minutes or 20:hours).\nEnter 'quit' to exit the program: """)
    if user_input.lower() == "quit":
        quit()
    days_and_unit = user_input.split(":")
    #print(days_and_unit)
    days_and_unit_dictionary = {"days": days_and_unit[0], "unit": days_and_unit[1]}
    #print(days_and_unit_dictionary)
    validate_and_execute()
Code language: Python (python)

Leave a Comment

4 × two =