hour = int(input("Starting time (hours): "))
mins = int(input("Starting time (minutes): "))
dura = int(input("Event duration (minutes): "))
#hour = 24
#mins = 60
mins = mins + dura # find a total of all minutes
hour = hour + mins // 60 # find a number of hours hidden in minutes and update the hour
mins = mins % 60 # correct minutes to fall in the (0..59) range
hour = hour % 24 # correct hours to fall in the (0..23) range
print(hour, ":", mins, sep='')
#π confused
28 messages Β· Page 1 of 1 (latest)
@soft stag
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
i was trying to solve this but ended up giving up because i couldn't figure it out
but going over the answer, how is mins & hour defined twice
wouldn't that just ignore the first definitions of mins & hour?
i thought python cared about defining a variable twice but when you do, the 1st variable becomes useless
not exactly
notice how the second assignment to mins references mins? that uses the value from the first assignment
so:
foo = 4 + 4 # afte this, foo == 8
foo = foo / 2 # foo is 8, so this is 8 / 2
foo will be 4 after the above
remebmer that in an assignment statement, the expression the right of = is evaluated first, and then the value that expression evaluates to is assigned to the name on the left
you probably want to use # for comments instead of // as that is another operator in python, especially here when doing calculations
thanks! I'm days-deep in a frontend project and the js has taken over
haha, yes, looked like you where coming from one of many many other language where // is for single line comments π
why can't you just do mins = mins + dura % 60 in one line
yo uabsolutely can
i at least had that part
breaking it up is purely an aesthetic preference here
i coudlnt figure out how to get the hour
it isn't useless unless you never use that value before overwriting the variable with a new value
there's mixed opinions here: on one hand, re-assigning variables is generally discouraged because it can make it hard to reason about the current value/meaning of a variable later in the code (was foo this thing, or that other thing?) and it can muddy type signatures. On the other, it can clear up what migh totherwise be complicated logic (I think your example falls into this category)
there is a function named divmod() that would probably be beneficial here that does both operations at the same time and yields two results
!d divmod
divmod(a, b)```
Take two (non-complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using integer division. With mixed operand types, the rules for binary arithmetic operators apply. For integers, the result is the same as `(a // b, a % b)`. For floating-point numbers the result is `(q, a % b)`, where *q* is usually `math.floor(a / b)` but may be 1 less than that. In any case `q * b + a % b` is very close to *a*, if `a % b` is non-zero it has the same sign as *b*, and `0 <= abs(a % b) < abs(b)`.
just to clarify, what result are you getting and what are expecting instead?
asking because as far as i can tell i think your code should be working even if it can be done in several other ways
!e - just as a demo, this does the same but without the input as it doesn't work here with the python bot
hours = 24
minutes = 60
duration = 2
hours, minutes = divmod(hours * 60 + minutes + duration, 60)
hours %= 24
print(f"{hours}:{minutes}")
:white_check_mark: Your 3.13 eval job has completed with return code 0.
1:2
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.