#๐Ÿ”’ round

73 messages ยท Page 1 of 1 (latest)

mighty rune
#

how do i round 0? so it shows 00, without making it 00:00 or 00:0, tried using the round() function but it didn't work that way, didnt work with :.f too

safe stumpBOT
#

@mighty rune

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.

kindred pewter
#

This is called padding, not rounding

mighty rune
#

oh

kindred pewter
#

!e ```py
print(f"{0:02d}")

mighty rune
#

second. ill try it

safe stumpBOT
#

@kindred pewter :white_check_mark: Your 3.12 eval job has completed with return code 0.

00
kindred pewter
#

using :2d will print " 0"

mighty rune
#

so :02d is "00"

#

ohhhhhh

#

shouldnt have added a 0 before the 2d

kindred pewter
#

no period

#

02d, not .02d

mighty rune
#

that too

#

oh so the 0 was ok

kindred pewter
#

.02f actually gives you 2 digits of percision after the decimal point

mighty rune
#

damn, i need to read into those..

#

i know how the f works but i guess there's more

kindred pewter
#

!d format

safe stumpBOT
#

format(value, format_spec='')```
Convert a *value* to a โ€œformattedโ€ representation, as controlled by *format\_spec*. The interpretation of *format\_spec* will depend on the type of the *value* argument; however, there is a standard formatting syntax that is used by most built-in types: [Format Specification Mini-Language](https://docs.python.org/3/library/string.html#formatspec).

The default *format\_spec* is an empty string which usually gives the same effect as calling [`str(value)`](https://docs.python.org/3/library/stdtypes.html#str).

A call to `format(value, format_spec)` is translated to `type(value).__format__(value, format_spec)` which bypasses the instance dictionary when searching for the valueโ€™s [`__format__()`](https://docs.python.org/3/reference/datamodel.html#object.__format__) method. A [`TypeError`](https://docs.python.org/3/library/exceptions.html#TypeError) exception is raised if the method search reaches [`object`](https://docs.python.org/3/library/functions.html#object) and the *format\_spec* is non-empty, or if either the *format\_spec* or the return value are not strings.
kindred pewter
#

!d str.format

safe stumpBOT
#

str.format(*args, **kwargs)```
Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces `{}`. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.

```py
>>> "The sum of 1 + 2 is {0}".format(1+2)
'The sum of 1 + 2 is 3'
```  See [Format String Syntax](https://docs.python.org/3/library/string.html#formatstrings) for a description of the various formatting options that can be specified in format strings.
mighty rune
#

thanks!

#

oh god that is long

#

sooo, a second issue popped up

#
print(f'{abs(remaining_time_early):02d} minutes before the start')```
kindred pewter
#

Maybe you want this? {x if x := abs(remaining_time_early) else "00"}

mighty rune
#

immm not sure

#

i started programming this week

#

i think i can just

#

have an outcome if its 00

kindred pewter
#

abs(remaining_time_early) or "00"

mighty rune
#

else

kindred pewter
#

that will use 00 if the time is 0

mighty rune
#

oh

#

but wouldnt that make it

#

ah i need a second to explain it but i think it wouldnt work?

#

hmm

#

i need an if for <10 since they all need a 0 behind

#

i think

#

and that would fix it

#

like same print just a 0 behind it

#

yeahhhh i used some other method @kindred pewter

if remaining_time_early<10:
         print("On time")
         print(f'{abs(remaining_time_early)} minutes before the start')
        else:
         print("On time")
         print(f'{abs(remaining_time_early):02d} minutes before the start')```
#

and it worked!

#

ty for the help

#

i just shouldnt :02d when i dont want a zero behind it

#

and in every other case i do :02d

kindred pewter
#

these do the same thing.

#

the only way remainint_time_early will be > 2 chars is if it's > 100

mighty rune
#

well i had issues with minutes

kindred pewter
#

and don't forget to apply abs() before checking if it's < 10

#

because -20 < 10

mighty rune
#

oh

#

sooo remaining_time_early and remaining_time_late

#

abs them before if

#

alright, thanks, noted

kindred pewter
#

!e ```py
for remaining_time_early in range(-11, 12):
print(f"On time {abs(remaining_time_early)} minutes before the start")

safe stumpBOT
#

@kindred pewter :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | On time
002 | 11 minutes before the start
003 | On time
004 | 10 minutes before the start
005 | On time
006 | 9 minutes before the start
007 | On time
008 | 8 minutes before the start
009 | On time
010 | 7 minutes before the start
011 | On time
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/6SSLZNFQWJZJAKDUYULLNB4VM4

kindred pewter
#

Was that what you wanted?

mighty rune
#

input is the hours + mins when a test started

#

and then input the hours + mins when a person arrived

#

says if u are late, on time or early

#

and by how much

kindred pewter
#

So if it's over an hour, include the minutes with 2 decimals

mighty rune
#

yeah

#

i meann

#

2 decimals = everything besides only minutes < 10

#

since hours arent 03:18, they are 3:18 for example

kindred pewter
#

You should make a function that converts minutes to a duration string

safe stumpBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.