#๐Ÿ”’ roast my shitty code

59 messages ยท Page 1 of 1 (latest)

inland lanternBOT
#

@solid nova

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.

solid nova
#

damn

#

well i said

tame belfry
#

!paste

inland lanternBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

solid nova
#

"my first big python project, it works but i think its spaghetti coded so have fun (you need these three for it to work, in the same dir)"

golden glade
#

you can put multiple files in the pastebin on the same link

solid nova
#

here it goes

#

dont ask why i chose this one

golden glade
#

!pep 8

inland lanternBOT
junior dove
#

I would break up functionality into functions instead of a big while loop that goes forever in your main function

golden glade
#

!eq

inland lanternBOT
#
Comparisons to `True` and `False`

It's tempting to think that if statements always need a comparison operator like == or !=, but this isn't true.
If you're just checking if a value is truthy or falsey, you don't need == True or == False.

# instead of this...
if user_input.startswith('y') == True:
    my_func(user_input)

# ...write this
if user_input.startswith('y'):
    my_func(user_input)

# for false conditions, instead of this...
if user_input.startswith('y') == False:
    my_func(user_input)

# ...just use `not`
if not user_input.startswith('y'):
    my_func(user_input)

This also applies to expressions that use is True or is False.

golden glade
#

it's definitely quite hard to follow the main function. 7 levels of indentation

solid nova
golden glade
#

also, not great that you have a line that is 378 characters long

solid nova
golden glade
#

in draw ascii function

solid nova
golden glade
#
soundPath: "./sound.wav" #if you are on windows, change that to .\sound.wav.

windows works with both slashes

#

at least powershell does, I didn't check cmd

solid nova
#

cmd doesnt im pretty sure

golden glade
#

it does, just checked

solid nova
golden glade
#

I also don't suggest to have a comment teaching people how file paths work in your config

clever radish
# solid nova here it goes

A) Really try and reduce the indentation. not only will it simplify the logic, it'd really help with the readability.

Look into using guard-if, Have the try-except encapsulate only the things that may raise the actual error.

B) split some of the repeated logic into their own separate functions.
you have a lot of repeated lines, with little to no variation amoung them.

C) Utilize the datetime library a bit more, I'd allow you to simplify and abstract a lot of the timing & hour calculation logic. along with .strftime()

solid nova
golden glade
#

you have to indent inside every compound statement, that is correct

#

that doesn't mean you can try to reduce how many you use nested

solid nova
#

yeah but i dont have to use tab everytime

golden glade
#

that's not what RandomName is getting at

#

you can reduce indentation by using the guard-clause style, and splitting stuff into functions

#

or changing the design in some way

#

not by doing this

if cond: statement
ancient kelp
solid nova
#

Well, imma apply that to my next projects, cuz now i don't really want to spend a bunch of time fixing the indentation

ancient kelp
#

Things like this:

        if timerPaused:
            pausePrint = "Unpause"
            resetPrint = "Reset"
        elif not timerPaused:
            pausePrint  = " Pause"
            resetPrint = " Reset"

can be:

solid nova
#

im happy that this poorly written thing had even worked

ancient kelp
#
        if timerPaused:
            pausePrint = "Unpause"
            resetPrint = "Reset"
        else:
            pausePrint  = " Pause"
            resetPrint = " Reset"

because you know it's "not timerPaused" in the else branch, because it didn't match the if branch.

solid nova
#

yep

#

also i couldve made the stopwatch behave the same as the alarm, so it keeps counting even when you go somewhere else, but i think that its not as important, because an alarm needs to be counting down at all times to be accurate

ancient kelp
#

You've got some code like this (I'm having trouble copy/pasting from your pastebin, might be my browser):

if mode == "timeout":
    ....
elif mode == "alarm":
    ....

I like to always end things like that with a final else to catch some mode not covered. Maybe that's fine, and you'd go:

if mode == "timeout":
    ....
elif mode == "alarm":
    ....
else:
    # other modes need nothing special
    pass

but often it means you've missed something:

if mode == "timeout":
    ....
elif mode == "alarm":
    ....
else:
    raise RuntimeError(f'unhandled {mode=}')

This catches mistakes like that and presents them to you loudly, with a stack trace so you can see where and how it was called, and showing the value of mode so that you know what value was not handled.

clever radish
ancient kelp
#

... rather than silently doing nothing, when something should have been done. Fail early, where the cause of trouble is easier to locate rather than later when weird behaviour's cause is not so obvious.

clever radish
#

@solid nova didn't ping ;-;

ancient kelp
solid nova
#

now it does tho

polar geyser
#

I'm just gonna knife my way in here, with zero context

#

Readability is much more important than performance, assuming you're making sane choices wrt to data structures and such

#

If you find yourself microptimizing you python, switch to C. You find yourself microptimizing your C, get some help

inland lanternBOT
#
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.