#๐ learning to debug
315 messages ยท Page 1 of 1 (latest)
@spring latch
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.
what is a gutter in this context?
point no 16
you mean a breakpoint
the red circle means you've set a breakpoint at line 16
ok
Do you understand for loop?
yes i do
OK so do you understand what columns[i] mean if i is 1 for example?
i previously put breakpoint on different lines and that time it didnt run to a random i
yes
Can you tell me what you understand what it does?
do i need to explain try, except too?
no. Just columns[i]. I need to know if you understood what that is. If you do, please explain it to me before we can proceed with the rest of the line
if loop is running and firstly its i becomes 1 and the if random.random is less than .2 then column[1] will be equal to a random number between 4 and 14 (both inclusive)
ok
So in your debugger, you have, from what I can see, 3 expressions that you are watching out for and see what values they are returning during the program run
WIDTH is 70
columns is a list of 70 numbers
and i is a number 62
So by following the for loop logic, i is now on the 62 iteration out of the 70 range from WIDTH
Then on this 62 iteration, the program stopped because the if condition random.random() < 0.02 is True and is now hitting the line 16 for setting columns[62] to a new random.randint number between 4 and 14
You can probably verify the if condition by adding random.random() and random.random() < 0.02 in the expression to watch for in your debugger
Enter here for those expressions. If you wanna watch for more expressions and values, you enter them here
how
oh ok
let me try
how do i enter
click in that box and type it in
remove the equal sign
just do random.random() and hit enter
And then do another expression random.random() < 0.02 and hit enter
what do you mean?
OK so you see in this screenshot that you shared, you typed random.random() = .12, right?
OK good. Press enter please
Then show me a screenshot of your current debugger
Pressing enter doesn't save that expression into the debugger?
Click here, and then press enter...
Or press this button
I think you've deleted the random.random() or something. But if you add the random.random() again, you can see what value it shows to tell you that the current iteration of random.random() < 0.02 is False
can i manually change this
Like this one. We can see what value is behind random.random() because its being watched in your Pycharm debugger. In this screenshot, its 0.0083627...
no.
Of course you can
you can have as many expressions and variables that you wanna watch in the debugger
This is like the better alternative than riddling your script with print() statements
This is much better!
So you can see now, at iteration i at 55, the random.random() has a value of 0.33 which is bigger than 0.02 so the random.random() < 0.02 is False
what if i changed the value of the if random.random() < 0.2
If you mean forcibly make it True, you can't.
oh
The values follows whatever is going on inside the running program in debug
like if the program is doing a = 1 + 1. And you are watching the variable a during debug. You would see that value shows as a = 2. You can't forcibly change that to 3 or "Bob" or whatever you want during the debug.
Does this make sense to you @spring latch ?
no it doesnt
what do you mean "no, it doesn't"?
watching a means the breakpoint is on a
oh it doesn't make sense to you? Sorry. What part is still confusing you?
No, watching a does not mean the breakpoint is on a
"watching" a variable or expression is to watch what values are being stored throughout the program run
The "breakpoint" is used to help stop the program when it reaches that line
You can "watch" a variable or expression without setting up breakpoints
Everything I explained to you, that is it. There's nothing more to learn in debugging
okay
Its not a big topic
i think i need to do practical on it..i got your points like 40 to 50%
Please ask if you feel that there is something in there that is still confusing you.
Maybe try a smaller script example?
sorry whats the question?
yes..a similar example
you can try a simple for loop and then watch the value changes as it loops
someone already explained that but
!d random.randint
random.randint(a, b)```
Return a random integer *N* such that `a <= N <= b`. Alias for `randrange(a, b+1)`.
It gives you a random number between 4 and 15
!e
import random
print(random.randint(4,14))
print(random.randint(4,14))
print(random.randint(4,14))
print(random.randint(4,14))
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | 8
002 | 5
003 | 11
004 | 12
See that? It gives me a random number between 4 and 14
i got this point
same idea. What do you think it means?
I asked you in the beginning about it. I thought you understood
does it mean column name will be changed to a no. btw 4 and 14 inclusive and after reading the remaining code, for e.g. it is 4 , then else will run and changed it into 0 or 1?
no
@spring latch what is i in this context?
column no
where does the number come from?
from length of width
columns = [0] * WIDTH
while True:
#loop over each column:
for i in range(WIDTH):
if random.random() < 0.02:
# Restart a stream counter on this column.
# the stream length is between 4 and 14 characters long.
columns[i] = random.randint(4,14)
let me show you the full code
You have a columns which is [0] * WIDTH
The WIDTH seems to be a constant of 70
So columns is [0] * 70
import random,sys,time
WIDTH = 70
try:
# For each column, when the counter is 0, no stream is shown.
# otherwise, it acts as a counter for how many times a 1 or 0
# should be displayed in that column
columns = [0] * WIDTH
while True:
#loop over each column:
for i in range(WIDTH):
if random.random() < 0.02:
# Restart a stream counter on this column.
# the stream length is between 4 and 14 characters long.
columns[i] = random.randint(4,14)
#print a character in this column
if columns[i] == 0:
# change this ' ' to '.' to see empty spaces:
print(' ', end='')
else:
#print a 0 or 1:
print(random.choice([0,1]), end='')
columns[i] -= 1 #Decrement the counter for this column
print() #print a newline at the end of the row of columns
time.sleep(0.1)
except KeyboardInterrupt:
sys.exit()```
70 zeroes inside columns
ok
You can see the value for columns in your Pycharm debugger with all of that zeroes
Next line is a while True
Do I need to explain while True for you or is that something you already understood?
yea pretty much
OK next line
for i in range(WIDTH):
Do you understand what this line is doing?
taking 70 and making it a range
alright and what is i doing in that line?
loops from 0 to 69
why does i loop from 0 to 69?
correct โ
i goes through the range of that WIDTH value
so since you know that then columns[i] should be going through the loops from 0 to 69 like columns[0], columns[1], etc
But!
It doesn't reach that line unless it goes through that if condition as True
which is a random.random() < 0.02
If its not True, then it doesn't go through that line columns[i] = random.randint(4,14)
ok
and then
Do you wanna keep going?
let me see the code again
Did you write these comments?
#print a 0 or 1:
print(random.choice([0,1]), end='')
columns[i] -= 1```
this part is what confusing me
no
copied from the book
ATBS]
Sorry I have to go
does it mean..
if i == no. between 4 and 14... does it mean this else block will run?
please, give me your sec
This line doesn't really do anything
print(random.choice([0,1]), end='')
It just prints a random 0 or 1
And that's it
ok
No impact or effect to the rest of the code
This line makes no sense
columns[i] -= 1 #Decrement the counter for this column
Sorry but I really have to go
ok..np...thanks for the help
If this help thread is still open, I'll check back later
i aprreciate your time
heard agent q had to leave, can you catch me up @spring latch ?
.
summarize what you wonder about now
breakpoint - it is the point where the code stops and i can see the steps at my pace
2. expression or add to watch - helps in checking the variable in detail(i am little confused with this
wait
i am summarizing
when it comes to debugging, there are a bunch off tools out there
i didnt get the else:
part
one of them is the pycharm debugger, and it is arguably the best debugger for python.
but python also has a debugger built in
and you can also debug your code without a debugger
so lets take this in these steps
lets start with how to debug without using any debugging tools
do you know how to do that ?
logging? printing?
yes
printing alone can do this, but there are things that are important to know about when you print out something for debugging purposes
have you given that any thoughts?
what things or information is important to see?
i tried to run this
let me simplify that a bit
import random
value = None
for i in range(10):
value = random.randrange(100)
now we want to debug valuewhat is one way to do that using print?
import random
value = None
for i in range(10):
value = random.randrange(100)
print(value)
alright.. lets see
!e
import random
value = None
for i in range(10):
value = random.randrange(100)
print(value)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | 29
002 | 37
003 | 74
004 | 36
005 | 71
006 | 14
007 | 50
008 | 1
009 | 26
010 | 91
what do we know about this value from this print?
what does value = None represents
ok
10 random ints from 1 to 100
do you know it is an int?
it has no upper commas
so you dont really know by just printing it?
so when you debug, you dont only want to see the human friendly value
you also want to see the name and the type
do you know how to get that using print?
how can you see the name, type and value using print?
yes, that will for sure work
print(value, type(value))
there is another way
want to see?
yes
oh!
sometimes you want the program to pause at a certain point while debugging. and for that you need a debugger, print just shows you
let me change the code a bit
import random
value = None
for i in range(10):
value = random.randrange(100)
if value > 75:
# i want a debugger breakpooint here
print(value)
is this a pycharm help post?
using only python, do you know how to set a breakpoint before the print?
just write breakpoint() there
yeah, and it will enter the python debugger
or something like importing the pdb and then pdb.trace or something
yes, but breakpoint() does that for you
using good names makes debuging simpler
the same tool you can activate in pycharm using the gutter and debug run
ok
so if you do that you will get the same info as python, but in an interface that shows you the stack much better.
let me start pycharm and show
ok
there is no issue is i write value = 0 instead of none right?
no issue no
thats what the continue command does
continues to the next breakpoint
in pycharm all navigation commands are here
oh thats an interesting topic
ok
when you use the python debugger, you have to write commands, like continue jump and so on
in the pycharm debugger you use these buttons or their shortcuts
that has already been mentioned
like they have to open their own thread right?
u guys wrote here a lot i ain't gonna read all this...
i'll just remain silent then i guess
is this things so far clear?
yes
so whats the point of adding a breakpoint?
how would you inspect?
by these buttons
do you have any other questions around debugging?
could just do c n ... and so on tho don't thin u'd need to write em full?
yes, the docs tell you about all variants
memorizing different buttons is harder imo..
!docs pdb
Source code: Lib/pdb.py
The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control.
The debugger is extensible โ it is actually defined as the class Pdb. This is currently undocumented but easily understood by reading the source. The extension interface uses the modules bdb and cmd.
what does this do..it is unclear to me
this is going to be the last topic..
do you know what a python expression is?
1 + 2
yes. and in this context, you can do this
what editor is that
ok, i wont take your more time..
i should go and just write
you can inject expressions into the debugger without having to write it in code and rerunn it
oh ok
notice i change i from 0 to 9
is it a good idea to memorize this code?
https://github.com/python/cpython/blob/3.14/Lib/pdb.py
no
damn:(
im pretty good at remembering things tho
its only like 3.5k lines.. i can go through over it in like a week
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.