Raizan - 1 month ago 9

Python Question

im new to python or programming in general so this might be simple but im stuck!

It would be great if anyone could help me! ^^

I want to be able to do a piece of code that does this:

- 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)

Hope you can understand :/

Thanks

Answer

You want the modulus function. This finds the remainder when dividing by the specified number. In this case, if the time was 25 hours, you would be left with 1. Or if the time was 30 hours for example, you would be left with six.

To implement this you would do: `print("Your plane leaves at: ", i % 24)`

Note that if the time is a multiple of 24 though, it will print 0.

Source (Stackoverflow)

Comments