python 运行程序时不显示任何内容[重复]

wvmv3b1j  于 2022-12-28  发布在  Python
关注(0)|答案(2)|浏览(194)
    • 此问题在此处已有答案**:

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.
g0czyy6m

g0czyy6m1#

您的convert函数没有return语句,因此隐式返回None。因此,None是分配给converted_time的值,然后打印该值。

mwyxok5s

mwyxok5s2#

原因是你既要在convert函数中打印,也要获取它的返回值并打印它。因为你没有从convert函数中返回任何东西,“None”被隐式返回,并被打印出来。你可以通过删除print(converted_time)调用来修复这个代码

相关问题