TIL: Python Basics Day 13 - Debugging Skills

이다연·2020년 12월 12일
0

Udemy Python Course

목록 보기
13/64

Debugging skills

removing bugs from your code

*comment, un-comment : ctrl + /

1. Describe the Problem

def my_function():
  for i in range(1, 20):
    if i == 20:
      print("You got it")
my_function()

<debugged>
# Describe Problem
def my_function():
  for i in range(1, 21):
    if i == 20:
      print("You got it")
my_function()

2. Reproduce the Bug

randint: endpoint doesn't deduct -1, unlike range, to reproduce the bug, we put end number to see if it produces error sign.

from random import randint
dice_imgs = ["❶", "❷", "❸", "❹", "❺", "❻"]
dice_num = randint(1, 6)
print(dice_imgs[dice_num])


#<debugged>
# Reproduce the Bug
from random import randint
dice_imgs = ["❶", "❷", "❸", "❹", "❺", "❻"]
dice_num = randint(0, 5)
print(dice_imgs[dice_num])
  

3. Play Computer

pretend you are a computer, and think like computer.
let's say input year is 1994, it deosn't belong to any of the condition.
if you put it in a if statement, 1994>1980 ? => True, 1994 >1994 ? => False, True and False => Flase

year = int(input("What's your year of birth?"))
if year > 1980 and year < 1994:
  print("You are a millenial.")
elif year > 1994:
  print("You are a Gen Z.")

<debugged>
# Play Computer
year = int(input("What's your year of birth?: "))
if year >= 1980 and year <= 1994:
  print("You are a millenial.")
elif year > 1994:
  print("You are a Gen Z.")

4. Fix the Error

Google it: TypeError: '>' not supported between instances of 'str' and 'int'

 # Fix the Errors
age = input("How old are you?")
if age > 18:
print("You can drive at age {age}.")

#TypeError: '>' not supported between instances of 'str' and 'int'

<debugged>
age = int(input("How old are you?: "))
if age > 18:
  print(f"You can drive at age {age}.")

5.Print is Your Friend

Print variables from time to time

# #Print is Your Friend
pages = 0
word_per_page = 0
pages = int(input("Number of pages: "))
word_per_page == int(input("Number of words per page: "))
total_words = pages * word_per_page
print(total_words)

#<debugged>
#Print is Your Friend
pages = 0
word_per_page = 0
pages = int(input("Number of pages: "))
word_per_page = int(input("Number of words per page: "))
total_words = pages * word_per_page
print(pages)
print(word_per_page)
print(total_words)

6. Use a debugger

Use a debugger
http://pythontutor.com/visualize.html#mode=display

#Use a Debugger
def mutate(a_list):
  b_list = []
  for item in a_list:
    new_item = item * 2
  b_list.append(new_item)
  print(b_list)

mutate([1,2,3,5,8,13])

#<debugged>
def mutate(a_list):
  b_list = []
  for item in a_list:
    new_item = item * 2
    b_list.append(new_item)
  print(b_list)

mutate([1,2,3,5,8,13])

7. Take a Break

if you starting at the code for a long time, then your brain won't work. Have some down time, and come back to it, everything gets so obvious.

8. Ask a Friend!

With fresh eyes, your friends can help you.

9. Run Often

Don't wait until you write loads of codes. Run and confirm.
leave it all to the end and end up with pile of bugs..

10. Ask/ Search StackOverflow

Bug or issue should be unique. if not, just search. Vital tool!
you only wanna ask a question when you've pretty sure that you've exhausted all other avenues of debugging and you've searched all of Stack Overflow.

Everone gets bugs. Important part of programmer's journey.

profile
Dayeon Lee | Django & Python Web Developer

2개의 댓글

comment-user-thumbnail
2022년 10월 18일

It is a great and nice commitment by the people those who are looking to move forward with the things here.
https://www.aliasharma.in/

답글 달기
comment-user-thumbnail
2024년 4월 26일

This is very interesting and your commitment genuine. I think you are so trusted -
https://www.delhicallgirls.net/
I am Jiya Roy a joy of babes in Kolkata.
https://jiyaroy.in/

답글 달기