所以我们应该提示用户输入时间为#:## a.m./p.m.,函数convert应该将输入的时间转换为浮点数,返回浮点数并使用打印足够的文本。
我收到一条错误,指出转换函数未返回十进制值,但当我手动输入时,终端正在通过所有测试。
出了什么问题?
def main():
text = input("time here: ")
time = convert(text) # call the function convert() and assign it to time variable, different from the one in the function
print(time, type(time)) # check if the function returns a float value
if 7 <= time <= 8: # simple logic for defining time
print("breakfast time")
elif 12 <= time <= 13:
print("lunch time")
elif 18 <= time <= 19:
print("dinner time")
elif time >= 24: # ensure that you can't type in number > 24
print("Not a valid time point.")
else:
None # print None (null) for other cases
def convert(time):
hour, minute, period = time.replace(":", " ").split(" ") # unpack the list with 3 arguments (3rd arugment is period = am/pm) and define floats
hour = float(hour)
minute = float(minute) / 60
n_time = hour + minute # assign to a new variable n_time which should return float
return round(n_time, 2) # return float, rounded to 2 decimals
if __name__ == "__main__":
main()
根据规格:
假设用户的输入将以 24 小时时间格式格式化为 #:## 或 ##:##
此行将因该输入而出错:
hour, minute, period = time.replace(":", " ").split(" ") # unpack the list with 3 arguments (3rd arugment is period = am/pm) and define floats
挑战说[强调]:
如果愿意接受挑战,可以选择添加对 12 小时时间的支持,允许用户以这些格式输入时间 too:
#:## 上午和 ##:## 上午
#:## 下午下午 ##:##
挑战必须处理任一格式。