#πŸ”’ confused

28 messages Β· Page 1 of 1 (latest)

soft stag
#
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='')
harsh riverBOT
#

@soft stag

Python help channel opened

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.

soft stag
#

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

foggy crystal
#

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

sullen jolt
foggy crystal
sullen jolt
soft stag
foggy crystal
soft stag
#

i at least had that part

foggy crystal
#

breaking it up is purely an aesthetic preference here

soft stag
#

i coudlnt figure out how to get the hour

sullen jolt
foggy crystal
#

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)

sullen jolt
#

!d divmod

harsh riverBOT
#

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)`.
sullen jolt
sullen jolt
harsh riverBOT
harsh riverBOT
#
Python help channel closed for inactivity

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.