#๐ roast my shitty code
59 messages ยท Page 1 of 1 (latest)
@solid nova
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.
!paste
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.
"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)"
you can put multiple files in the pastebin on the same link
!pep 8
I would break up functionality into functions instead of a big while loop that goes forever in your main function
!eq
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.
it's definitely quite hard to follow the main function. 7 levels of indentation
yeah InputKeys() does that alot but i changed that now
also, not great that you have a line that is 378 characters long
as i said its a painful experience
which one
in draw ascii function
oh yeah
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
really? i didnt use windows in quite a while
cmd doesnt im pretty sure
it does, just checked
LOL imma remove that comment then
I also don't suggest to have a comment teaching people how file paths work in your config
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()
Well when I went into python i thought i had to use tab every time, and then i just went with it
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
yeah but i dont have to use tab everytime
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
Just to this: (a) you can make TAB stops whatever width you like (8 is traditional and 4 is common) and (b) you can use spaces for smaller indents, though 4 space indents are the common convention (I use only 2 in my personal code).
All you actually need is for an indented block to be indented at least 1 space, and consistiently so in that block.
Well, imma apply that to my next projects, cuz now i don't really want to spend a bunch of time fixing the indentation
Things like this:
if timerPaused:
pausePrint = "Unpause"
resetPrint = "Reset"
elif not timerPaused:
pausePrint = " Pause"
resetPrint = " Reset"
can be:
im happy that this poorly written thing had even worked
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.
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
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.
A nice video I found about reducing nesting-
https://youtu.be/CFRhGnuXG-4?t=1m35s
I'm a Never Nester and you should too.
Access to code examples, discord, song names and more at https://www.patreon.com/codeaesthetic
Correction: At 2:20 the inversion should be "less than or equal", not "less than"
... 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.
@solid nova didn't ping ;-;
I do feel we shouldn't need to ping. But yeah.
well, i re did the easter egg, because it didn't work
now it does tho
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
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.