如果输入间隔在另一个间隔内,则追加到列表

If an input interval is within another interval, append to list

我正在编写一个程序,根据用户输入的日期和时间,通知用户在参观动物园期间哪些动物会醒来并喂食。 我有一个 class,一个读取文件的函数,该文件的信息包括姓名、清醒时间、喂食时间以及其他一些有用的东西。

 class Animal:
      def __init__(self, name, sleep, diet, awake, feed, number):
           self.name = name
           self.sleep = sleep
           self.diet = diet
           self.awake = awake
           self.feed = feed
           self.number = number
      def __repr__(self):
           return self.name + " " + self.sleep + " " + self.diet + " " + str(self.awake) + " " + str(self.feed) + " " + str(self.number)

 def readInfo():
      infile = open("zoo.txt", "r", encoding="UTF-8")
      animals = []
      lines = infile.readlines()
      infile.close
      for line in lines:
           lineparts = line.split(" / ")
           name = lineparts[0]
           sleep = lineparts[1]
           diet = lineparts[2]
           awake = lineparts[3]
           feed = lineparts[4]
           number = lineparts[5]
           animals.append(Animal(name, sleep, diet, awake, feed, number))
      return animals

 def Awake(x):
      awakeanimals = x
      print("\nYou can see ")
      for object in awakeanimals:
           print(object)

 def Feed(x):
      matadjur = x
      print("\nand you can feed: ")
      for object in feedanimals:
           print(object)

这是我正在努力处理的代码:

def open():
     animals = readInfo()
     awake = list()
     feed = list()
     time = int(input("Enter a time interval, eg 07-16")).split("-")

     if 9 <= time <= 20:
          awakeanimals.append(animals[0].name)
     if 12 <= time <= 14:
          awakeanimals.append(animals[1].name)
     if 21 >= time >= 05:
          awakeanimals.append(animals[2].name)
     #same for the rest of the animals

     if time <= 12 <= time:
          feedanimals.append(animals[0].name)
     if time <= 13 <= time:
          feedanimals.append(animals[0].name)
     #same for the rest of the animals

Awake(awakeanimals)
Feed(feedanimals)

之后我有一个简单的菜单,它根据用户输入的日期调用函数 open() 并继续时间部分。

我不知道如何在 if 条件下获得正确的输入。

此外,由于 ValueError: invalid literal for int() with base 10time = input().split("-") 不起作用,所以我正在考虑使用两个时间输入,time1 = input()time2 = input()。但是,将其纳入 if 条件似乎更复杂。

我不太确定你想要的 if 逻辑。假设它在一个范围之间进行检查,这里是东西

split("-") 失败,因为您在整数值上调用它。 split 只能应用于字符串。并且 base 10 错误发生,因为 07-16 不是有效数字。

这是一段经过编辑的代码

def open():
    animals = readInfo()
    awake = list()
    feed = list()

    start_time, end_time = sorted(map(int, input("Enter a time interval, eg 07-16").split("-")))
    # time will be a tuple like (7, 16). We are sorting so that min value is first always


    if 9 <= start_time and end_time <= 20:
        awakeanimals.append(animals[0].name)
    if 12 <= start_time and end_time <= 14:
        awakeanimals.append(animals[1].name)
    if 21 >= start_time and end_time >= 05:
        awakeanimals.append(animals[2].name)
    # same for the rest of the animals

    if start_time <= 12 <= end_time:
        feedanimals.append(animals[0].name)
    if start_time <= 13 <= end_time:
        feedanimals.append(animals[0].name)
    # same for the rest of the animals

这主要适用于输入问题和比较。