Now there are programs that sometime might execute and sometime they might throw an error. Now that puts us in a weird situation.
like bro why do it work sometime and not other time?
Follow the following example
from random import randint
numbers = ["1", "2", "3", "4", "5", "6"]
select_a_number = randint(a: 1, b: 6)
print(numbers[select_a_number])
Run this code, and you'll notice that it works most of the time but ocasionally throws an error.
What is our job? To find the bug duh
- We have to find when does that error actually happen?
- Try reproducing the bug yourself. (sometimes to destroy the monster you must reproduce the monster)
- Try to notice when it happens, what conditions lead up to the bug/error.
- Change the code so that it always produces the error.
Look at the following line of code:
select_a_number = randint(a:1,b:6)
This randint function chooses a number between 1 and 6 including the 1 and the 6 (This is not like the range function where it did not include the last number in the range.) When a number is chosen it is inserted into the numbers list to select a number from the list and print it.
print(numbers[select_a_number])
- now when we pick a number from our list we are ocasionally getting an error.
- This can point to the randint function being used
- We look at the amount of numbers in a list that is 6.
- We look at the randint which picks the number with the range from 1 to 6(including the 6)
We will try picking the numbers in the list manually, to select each individual number. Putting 1 in the list.
select_a_number = 1
the output will be
output = 2
We repeat this untill we find the bug but also keep your eyes open for any weird behaviour like when we placed 1 in the list it selected the number 2(which is the second item in the list)
- Putting 2 in the list.
select_a_number = 2
the output will be
output = 3
- Putting 3 in the list.
select_a_number = 3
the output will be
output = 4
- Putting 4 in the list.
select_a_number = 4
the output will be
output = 5
- Putting 5 in the list.
select_a_number = 5
the output will be
* Putting 6 in the list.
output = 6
select_a_number = 6
the output will be
output = ERROR!!!!!!!!!
We have finally reproduced the error, The problem here is that the list index starts from 0 hence we never saw 1 being printed and for 6 elements it would go from 0 to 5 but the randint function is going 1 to 6. which tells us there is one element that is never being selected and at one index i.e., 6 there is no element which is the reason for our bug.