一直减法直到我得到一个小于24的数字

2024-04-19 15:43:31 发布

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

我不熟悉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

^{pr2}$

希望你能理解:/

谢谢


Tags: andto代码addinput编程floatflight
3条回答
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行,使用%而不是/进行除法得到余数,而不是除法的结果:)

这叫做模除

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小时),它给出了操作给出的小时数。我想这就是它的作用

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

要实现这一点,您可以这样做:print("Your plane leaves at: ", i % 24)

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

相关问题 更多 >