我是python或编程的新手,所以这可能很简单,但我卡住了! 如果有人能帮助我会很棒! ^^
我希望能够做一段执行此操作的代码:
需要2个输入:x和y
x = float(input("Hours(00 to 24):\n"))
y = float(input("Hours until flight:\n"))
然后我想将它们添加到一起,以便值在00:00之间 和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)
希望你能理解:/
由于
答案 0 :(得分:1)
你想要模数函数。这会在除以指定数字时找到余数。在这种情况下,如果时间是25小时,那么你将留下1.或者如果时间是30小时,那么你将剩下六个。
要实现这一点,您可以:print("Your plane leaves at: ", i % 24)
请注意,如果时间是24的倍数,则会打印0。
答案 1 :(得分:0)
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行,使用%
代替/
进行除法得到余数而不是除法结果:)
它被称为模数除法
答案 2 :(得分:-1)
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小时),它会给出操作给出的小时。我认为这就是它的作用