def get_time():
start_time = str(input("Please enter the time you started working (hh:mm) "))
if ":" in start_time:
h1, m1 = start_time.split(":")
else:
h1 = int(start_time)
m1 = " "
if h1 < 8:
print("You can't start working until 08:00.")
elif h1 > 23:
print("You can't work past 24:00 (midnight) ")
end_time = str(input("Please enter the time you stopped working (hh:mm) "))
get_time()
这是我的一个程序的代码,我打算接受有人照顾的时间。我无法将字符串数字转换回整数。我收到错误:
File "/Applications/Python 3.4/babysitting.py", line 10, in get_time
if h1 < 8:
TypeError: unorderable types: str() < int()
为什么没有h1 = int(start_time)
工作?
答案 0 :(得分:3)
为什么
h1 = int(start_time)
没有工作?
当输入中有:
个字符时,该行根本没有被执行:
if ":" in start_time:
h1, m1 = start_time.split(":")
else:
h1 = int(start_time)
m1 = " "
当输入中没有int(start_time)
时,:
仅执行 ,因此当if
测试为假时。
分离拆分和整数转换:
h1 = start_time
if ":" in start_time:
h1 = start_time.split(":")[0]
h1 = int(h1)