#๐Ÿ”’ Python

68 messages ยท Page 1 of 1 (latest)

little anchor
#

Hello I'm begganer
Isn't in this code the result should be (4)
Because it's (0.4) so why it's 1200
And how to solve this

remote needleBOT
#

@little anchor

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.

meager swift
#

1200 divided by 3000 is 0.4 you are right, but
As 1200 is less than 3000, it cannot divide even once, so the remainder is 1200

maiden veldt
#

a%b==c when a = b*(a//b)+c

1200//3000 is 0
So the remainder is 1200-0*3000 = 1200

#

I don't know why you think it should be 4 "because it's 0.4"
1200/3000 (normal division) is 0.4... But that doesn't have any *10 there? pithink

Or were you thinking that a%b means "how many percent of b is a?" - but then you'd expect 40, not 4...

neat glade
#

in other words, what did you think the % operator does @little anchor ?

uneven root
neat glade
#

yes, and if you divide 1200 by 3000, how many times does 3000 fit in 1200, and how much is left over after the division?

little anchor
#

I tried to do like how many hours and minutes in 5000 second but look at last line it says wrong

neat glade
#

can you please at least answer my question?

#

also, please learn how to use the screen capture tool rather than taking photos of your screen

little anchor
neat glade
#

what you probably want is divmod()

#

!e

hours, seconds = divmod(5000, 3600)
minutes, seconds = divmod(seconds, 60)
print(f"{hours} h, {minutes} m, {seconds} s")
print(hours * 3600 + minutes * 60 + seconds)
remote needleBOT
little anchor
#

Sorry I don't know much in laptop can you help with this picture

neat glade
#

!e

# an alternative way
minutes, seconds = divmod(5000, 60)
hours, minutes = divmod(minutes, 60)
print(f"{hours} h, {minutes} m, {seconds} s")
print(hours * 3600 + minutes * 60 + seconds)
remote needleBOT
neat glade
#

does this way make more sense to you?

little anchor
neat glade
#

I gave you a direct link to its documentation โ€“ did you read it?

#

it makes a division and returns you the quotient and the remainder in one shot

#

quite convenient for operations like this

little anchor
little anchor
#

Thank you

neat glade
#

it's not a complicated function

#

!e

print(divmod(10, 3))
#

!e

print(divmod(10, 3))
remote needleBOT
neat glade
#

so 10 // 3 == 3 and 10 - 10 // 3 == 1, easy enough?

#

let me know if there's something you don't understand

cedar frost
#

!e print((10 - 10 // 3) == 1)

remote needleBOT
neat glade
#

woops

#

I suppose 10 - 10 // 3 * 3 == 1 would have been better, but that does get a bit complex looking

#

it was intended to show that the remainder from the integer division would be 1

neat glade
#

alright โ€“ if there's nothing else, you can do !close

cedar frost
#

divmod() is just a shortcut for // and % combined, eg:

minutes = seconds // 60
seconds = seconds % 60

# is equal to

minutes, seconds = divmod(seconds, 60)
little anchor
cedar frost
#

BTW, in your example:

hours = seconds // 3600
rest_hours = seconds % 3600

the rest_hours are still seconds and not minutes like you have printed.

little anchor
#

Buy why

cedar frost
#

Because you used the seconds, a modulo operation will not convert them to minutes.

#

1 hour == 3600 seconds, 5000 - 3600 == 1400 but still seconds

little anchor
#

I typed (+str (rest_hour ) which is the reminder of (5000/3600) that is should be (38)

#

Because 5000/3600 is 1.38

cedar frost
#

!e

print(5000 / 3600)
print((5000 / 3600 - 1) * 3600)
remote needleBOT
cedar frost
#

So, no -> 5000 % 3600 is 1400

#

The remainder of a division is not the fraction part of the result. It is the whole number which does not get used by the division.

little anchor
#

Now it's done thank you๐Ÿฅณ๐Ÿฅณ

cedar frost
#

It would be more accurate to name them rest_secondes (and rest_minutes).

honest elk
#

@little anchor

#

go to windows search

#

bottom left of your screen Type here to search

#

and then type snipping tool

#

then click the program that shows up

remote needleBOT
#
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.