#πŸ”’ Beginner Trying To Learn To Debug

87 messages Β· Page 1 of 1 (latest)

burnt cypress
#

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?

karmic cragBOT
#

@burnt cypress

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.

cerulean depot
burnt cypress
#

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?

primal yacht
#

Show us an example I mean of the code that isn't working

cerulean depot
# burnt cypress Even if the code isnt 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.

burnt cypress
#

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

primal yacht
#

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

#

?

burnt cypress
#

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.

primal yacht
#

its okay

#

So I assume it is not importing to the other file.

#

Can I see your file structure

#

?

burnt cypress
#

Yes. Do you need the main game file or all 8 files? Or just the scoreboard files?

primal yacht
#

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?

burnt cypress
#

Yeah, I will just let you show an example. I don't have Discord Nitro and it is too long.

primal yacht
#

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

main.py

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

burnt cypress
#

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?

primal yacht
#

I assume the first scoreboard is the filename

burnt cypress
#

Yes thats it name, sorry for misunderstanding "untils', I am a nitwit.

primal yacht
#

Everyone starts somewhere

#

Let me use ai to generate a new file tree and show you another example

tawny fulcrum
#

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

primal yacht
#

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.

primal yacht
tawny fulcrum
primal yacht
#

assuming I am in the project directory?

burnt cypress
#

from folder2.file_1_in_folder_2 import function _2 Something kinda like this?

primal yacht
#

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

burnt cypress
#

I got it. lol

primal yacht
#

Do you know how that works

burnt cypress
#

My code that isnt working?

primal yacht
#

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

burnt cypress
#

I messed up that post I am sorry

primal yacht
#

Its okay quick question are there other files

burnt cypress
#

That should be all of them there are 8 in total.

primal yacht
#

Are you declaring the classes or whatever the technical term is?

burnt cypress
#

Yes.

primal yacht
#

import pygame also this should be import pygame

#

no need to have spaces

primal yacht
burnt cypress
#

I thought I had to make a Class for the ship, bullets, and aliens in the game. Am I not doing them right?

primal yacht
#

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

burnt cypress
#

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

primal yacht
#

I know nothing about pygame

#

did that help?

#

Just create a different file and import the classes and initialize them?

burnt cypress
#

Yes it did. I will give this a try and see if it corrects my issue. I greatly appreciate the help and your time.

primal yacht
#

NP create a new post if it doesn't work someone will answer

burnt cypress
#

Thank you and have a good night!

primal yacht
#

same

karmic cragBOT
#
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.