Python代码未给出正确的结果

2024-05-16 09:29:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python中的这段代码有问题。我需要把时间改成军事时间吗,怎么改?我的代码没有返回正确答案

问题是: 小时=3 分钟=45

#Around Georgia Tech, there are plenty of places to get a
#late night bite to eat. However, they have different hours,
#so when choosing where to go, you have to think about who's
#still open!
#
#Imagine you're choosing between the following restaurants:
#
# - Barrelhouse: Closes at 11:00PM
# - Taco Bell: Closes at 2:00AM
# - Cookout: Closes at 3:00AM
# - Waffle House: Never closes. Ever.
#
#Assume that this list is also a priority list: if Barrelhouse
#is open, you choose Barrelhouse. If not, you choose Taco Bell
#if it's open. If not, you choose Cookout if it's open. If
#not, you choose Waffle House.
#
#However, there are two wrinkles:
#
# - We're using 12-hour time.
# - hour will always represent a time from 10PM to 5AM.
#
#That means that if hour is 10 or 11, it's PM; if hour is
#12, 1, 2, 3, 4, or 5, it's AM. This will make your reasoning
#a little more complex. You may assume that all four
#restaurants open later than 6AM, though, so you don't have
#to worry about opening time, just closing time.
#
#Add some code below that will print what restaurant you'll
#go to based on the current values of hour and minute.

我的代码在这里

#Add your code here!

if hour == 12 and hour <= 5 and hour >= 1:
    print("Taco Bell" , "Cookout", "Waffle House")
elif hour > 5 and hour < 11:
    print("Waffle House" , "Barrelhouse")

Tags: andto代码youifthattimeis
2条回答
if hour > 6 and hour < 11:
    print("Barrelhouse")
elif (hour > 6 and hour < 12) or (hour >= 12 and hour < 13) or (hour >= 1 and hour < 2):
     print ("Taco Bell")
elif (hour > 6 and hour < 12) or (hour >= 12 and hour < 13) or (hour >= 1 and hour < 3):
    print ("Cookout")
else:
    print("Waffle House")

您应该具有以下流程才能获得餐厅列表

list=[]

list.append("Waffle House")
if hour<=2 or hour>=6 :
    list.append("Taco Bell")

if hour<=3 or hour>=6:
    list.append("Cookout")

if hour<=11 and hour>=6:
    list.append("Barrelhouse")    

print(list)

相关问题 更多 >