I am new and following along with a Python book. I am wondering how one goes about debugging something that doesn't work, but doesn't stop the small game I have coded from working. I am using Visual Code Studio and I know it has a debugging feature, but I am too stupid to understand it. Can someone please just give me so guidance on how I should properly debug things like this, or do I just need to study more and it will eventually come to me?
#π Beginner Trying To Learn To Debug
87 messages Β· Page 1 of 1 (latest)
@burnt cypress
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.
Closes after a period of inactivity, or when you send !close.
Welp, the first thing you ask to yourself is "what's going wrong?" and then after that "do I know where it might be going wrong?" If you answer yes to that last one, and even then, you can usually go about just placing print statements in that part of your code that communicate useful information to you that can help you find the bug.
So placing a print statement at any part of the code will print information about that part of the code in the terminal?
Even if the code isnt working?
Show us an example I mean of the code that isn't working
It would probably help if you show us the code. If the code is not working as in it's actually not running, you should get an error report; if the code is not working as in it's not doing what's expected, then yeah, you can use prints.
You'd put those prints in places of interest, places where you think stuff might be going wrong.
It is a little game but it has several files the main code pulls from. The highscore board implemented shows a score but is not saving it. From the book I am following, it should save the score, but it is not. I do not have enough knowledge of Python to really know what took look for I am afraid. Do I put just the scoreboard file, or all the files that import it?
It throw up no errors, it just is not properly saving the highscore
How are you suppose to save it? Do you mean in a database , in a variable?
Are you using pure python
?
Do you know how python imports work to different files
?
I am using VS Studio if thats what you mean, I am not sure what pure Python means I am sorry. I have a settings file, game stats file, and a scoreboard file all have code pertaining to the highscore, but they are all imported into the main game file. I know very little about how they work. I just know I have code in another file that can be imported to the main file. Sorry for my lack of knowledge.
its okay
So I assume it is not importing to the other file.
Can I see your file structure
?
Yes. Do you need the main game file or all 8 files? Or just the scoreboard files?
Just show it all
the file structure
I mean what files are where
if you want I can just show you how imports work in python?
Yeah, I will just let you show an example. I don't have Discord Nitro and it is too long.
Just give me a sec
Okay first example I used ai to generate the structure
Now lets assume I am already in the project folder
below is the route.
...\project
project/
β
βββ main.py
βββ utils.py
Here is the functions utils.py
def hello_world
print('hello world')
If I was importing to main.py from utils.py I would simply go
from utils import hello_world`
helloworld
Sorry I got a phone call for a sec
Now do you want me to show from different directories?
Basically the pattern is filename import function or variable or class
Also if I had more then 1 function for example I would simply go
from utils import hello_world_1, hello_world_2
No problem. So is the "utils" important? The book I am following has import commands that look like "from scoreboard import Scoreboard" I have never seen the untils command. Really dumb question, what is the difference between a separate file and a directory?
Yes thats it name, sorry for misunderstanding "untils', I am a nitwit.
Everyone starts somewhere
Let me use ai to generate a new file tree and show you another example
i'm not sure
i feel like if the file wasnt being imported errors would be thrown
for example if they're trying to call a highscore save function from a highscore.py file, an error would be thrown if it isn't imported properly
i feel like since no errors were thrown you should look at the highscore saving function itself
project/
β
βββ folder1
β βββ file_1_in_folder_1
β
βββfolder2
βββ file_1_in_folder_1
βββ file_2_in_folder_2
Here are the contents of
file_1_in_folder_1,py
def function_1()
print('This is function_1')
file_2_in_folder_1,py
def function_2()
print('This is function_2')
file_1_in_folder_2,py
def function_3()
print('This is function_3')
Now I will use this example to import a file from folder 1 and folder 2
Assume I am in the "project" directory like last time.
I am importing to "folder1" and the "file" is file_1_in_folder_2 and "function" is "function_2"
folder1/ file_1_in_folder_1.py
from folder1.file_1_in_folder_1 import function_2
function_2
And the output I get is function_2.
I agree but the person doesn't even know how python imports work at least let me teach them that
alright. sounds good i'll leave you to it, lol
Now how would I import the function in folder2 to file_1_in_folder_2 to file_1_in_folder_2?
assuming I am in the project directory?
from folder2.file_1_in_folder_2 import function _2 Something kinda like this?
typo on my part
Now how would I import the function in folder2 to file_1_in_folder_2 to file_2_in_folder_2?
If you got it you don't have to answer
Just state I got it
I got it. lol
https://paste.pythondiscord.com/ post the code in pastebin
Do you know how that works
My code that isnt working?
yes everything
You can do it in 1 file
just label the files and folders
before the code
I will take a look if I can't solve it just post a new post
Sorry I just wanted to explain imports
in python
Its okay quick question are there other files
That should be all of them there are 8 in total.
Are you declaring the classes or whatever the technical term is?
Yes.
Where?
I thought I had to make a Class for the ship, bullets, and aliens in the game. Am I not doing them right?
I assume so but you instantiating the class. Check this specifically "instantiating the class" part https://www.reddit.com/r/learnpython/comments/83fk1i/can_someone_eli5_what_def_init_self_means_i_still/
right link check wolfgee answer
Basically you are creating the blueprints but not building the object from the blueprints
I could be wrong
I never used pygame before
Ok, that's a good way of explaining it. It makes more sense to me now. I obviously don't know enough about pygame. lol
I know nothing about pygame
did that help?
Just create a different file and import the classes and initialize them?
Yes it did. I will give this a try and see if it corrects my issue. I greatly appreciate the help and your time.
NP create a new post if it doesn't work someone will answer
Thank you and have a good night!
same
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.