- 此问题在此处已有答案**:
What is the purpose of the return statement? How is it different from printing?(15个答案)
20小时前关门了。
每当我试图运行我的程序,它的工作,但没有也显示后。以下是代码和结果,我得到的。
def main():
time = input("What time is it: ")
converted_time = convert(time)
print(converted_time)
def convert(time):
hours,minutes = time.split(":")
new_hours = float(hours)
new_minutes = float(minutes)
if new_hours >= 7 and new_hours <=8:
print("Breakfast Time")
elif new_hours >=12 and new_hours <=13:
print("Lunch Time")
elif new_hours >=18 and new_hours <=19:
print("Dinner Time")
else:
print("")
if __name__ == "__main__":
main()
What time is it? 7:00
answer:
Breakfast Time
None
tried playing around with some of the print functions.
2条答案
按热度按时间g0czyy6m1#
您的
convert
函数没有return
语句,因此隐式返回None
。因此,None
是分配给converted_time
的值,然后打印该值。mwyxok5s2#
原因是你既要在convert函数中打印,也要获取它的返回值并打印它。因为你没有从convert函数中返回任何东西,“None”被隐式返回,并被打印出来。你可以通过删除
print(converted_time)
调用来修复这个代码