This is more of a why, rather than a "how do I fix this" question.
I'm attempting to take an input of time, whether seconds, minutes, hours, or days, and then return the number of seconds that measurement is equal to with this code:
#!/usr/bin/env python3
valid_measurements = {
"s": "1",
"m": "60",
"h": "3600",
"d": "86400"
}
def print_err(err_type):
if err_type == "time_format":
print('\tTime should be entered as s/m/h/d')
print('\t\ts = seconds')
print('\t\tm = minutes')
print('\t\th = hours')
print('\t\td = days')
print('\tFormat: "30s" or "20m" or "1h"')
print('\tFormat: "30 s" or "20 m" or "1 h" ')
def input_time(time_type):
time_value = 0
multiplier = 0
time = input("Enter " + time_type + ": ")
if time[-1] in valid_measurements:
measurement = time[-1]
time_value = int(time[0:-1].rstrip())
multiplier = int(valid_measurements[measurement])
else:
print_err("time_format")
# For some reason this returns 0 for either value
input_time(time_type)
return time_value * multiplier
def main():
work_time = input_time("Work Time")
break_time = input_time("Break Time")
print("Real work time: " + str(work_time))
print("Real break time: " + str(break_time))
main()
However, I get this output when I attempt to break the code:
Enter Work Time: 20
Time should be entered as s/m/h/d
s = seconds
m = minutes
h = hours
d = days
Format: "30s" or "20m" or "1h"
Format: "30 s" or "20 m" or "1 h"
Enter Work Time: 20m
Enter Break Time: 5m
Real work time: 0
Real break time: 300
Process finished with exit code 0
Why does real work time return 0, despite me using the correct format for the input on the second round of the function when it calls itself? Additionally, is there anything I can do to clean up this code and make it more efficient or python-esque?