[英]Can't change a string back to an integer in Python 3
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()
Here's my code for a program I'm making to take in the times someone babysitted. 这是我正在接受某人看护的程序的代码。 I'm having trouble turning the string numbers back into an integer. 我在将字符串数字重新转换为整数时遇到麻烦。 I get the error: 我得到错误:
File "/Applications/Python 3.4/babysitting.py", line 10, in get_time
if h1 < 8:
TypeError: unorderable types: str() < int()
Why isn't h1 = int(start_time)
working? 为什么h1 = int(start_time)
不起作用?
Why isn't
h1 = int(start_time)
working? 为什么h1 = int(start_time)
不起作用?
That line isn't being executed at all when you have a :
character in the input: 当输入中包含:
字符时,该行根本不会执行:
if ":" in start_time:
h1, m1 = start_time.split(":")
else:
h1 = int(start_time)
m1 = " "
The int(start_time)
is executed only when there is no :
in the input, so when the if
test is false. 仅当输入中没有:
时, 才执行int(start_time)
,因此, if
test为false。
Separate the splitting and the integer conversion: 将拆分和整数转换分开:
h1 = start_time
if ":" in start_time:
h1 = start_time.split(":")[0]
h1 = int(h1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.