继续减去直到我得到一个小于 24 的数字

Keep subtracting until i reach a number less than 24

我是 python 或一般编程的新手,所以这可能很简单,但我卡住了! 如果有人能帮助我,那就太好了! ^^

我希望能够编写一段代码来执行此操作:

takes 2 inputs: x and y

x = float(input("Hours(00 to 24):\n"))

y = float(input("Hours until flight:\n"))

then i want to add them together so that the value is between 00:00 and 24:00

    i = (x+y)

  if i < float(24):

    print ("Your plane leaves at:",i)

  elif i > float(24):

 do something that keeps the result always less than 24:00h so that even if its 14:00h and your flight is in 51 hours the result isnt 65:00h but 17:00h (dont need the 2 days that went by, just the hour)

希望你能看懂:/

谢谢

def function(x,y):  # x and y as defined by OP
    i = x + y  # sets i as defined by OP
    if i < 24.0  # does the bit that OP asked for if i<24
        print("Your phone leaves at:", i)
    elif i > 24.0  # does the other bit op asked for
        i = i % 24.0  # get the remainder of dividing i by 24
        print("Your phone leaves at:", i)  #  prints the result of the last one

应该做,没测试过。

您需要了解的主要部分是第 6 行,使用 % 而不是 / 进行除法得到余数而不是除法结果:)

这叫模除法

你想要取模函数。除以指定的数字时,这会找到余数。在这种情况下,如果时间是 25 小时,您将剩下 1 个。或者如果时间是 30 小时,例如,您将剩下 6 个。

要实现这个你会做:print("Your plane leaves at: ", i % 24)

请注意,如果时间是 24 的倍数,它将打印 0。

x = float(input("Horas atuais(00 até 24):\n"))
y = float(input("Horas que faltam para o voo:\n"))
i = x + y

if i < float(24):
    print ("O seu aviao parte ás",i)
elif i > float(24):
    a = i % 24
    print ("O teu aviao parte ás",a)
elif y == float(24):
    print (x)
  • 这就是我要输入的内容...我添加了最后一个 elif,这样如果飞机起飞前的时间是空洞日(24 小时),它会给出操作员给出的小时数。我认为这就是它的作用