Next up is adding the ability to enter a comma separated list to the script, so that multiple values can be processed at once. The list will still be validated, and if there are values that are not positive integers they will be ignored.
calculation_to_units = 24
name_of_unit = "hours"
def days_to_units(number_of_days):
if number_of_days > 0:
return f"{number_of_days} days are {number_of_days * calculation_to_units} {name_of_unit}"
else:
return "You did not enter a positive integer..."
def validate_and_execute(validated_days):
if validated_days == "quit":
quit()
try:
print(days_to_units(int(validated_days)))
except ValueError:
print("Please enter a positive integer value...")
days_entry = 0
while days_entry != "quit":
days_entry = input("Enter the number of days, and enter 'quit' to exit the program: ")
for num_of_days in days_entry.split(","):
validate_and_execute(num_of_days)
Code language: Python (python)