#How to quickly be able to identify and remove these bugs
#1.Describe the Problem
def my_function():
for i in range(1,20):#fix the code:range(1,21)
if i == 20:
print("You got it")
my_function()
#2.Reproduce the Bug
from random import randint
dice_imgs = ["1","2","3","4","5","6"]
dice_num = randint(1,6)#fix the code:randint(0,5)
print(dice_imgs[dice_num])
#3.Playing Computer
year = int(input("What's your year of birth?"))
if year > 1980 and year < 1994:
print("You are a millenial.")
elif year >1994:#fix the code:year >= 1994
print("You are a Gen Z.")
#4.Fix the Errors
age = input("How ole are you?")
if age > 18:
print("You can drive at age {age}.")
#Fix the code:
age=int(input("How old are you?"))
if age > 18:
print(f"You can drive at age {age}.")
#select the parts of the error,and then copy it and paste it into Google
#5.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)
#print(f"pages = {pages}")
#print(f"word_per_page = {word_per_page}")
#word_per_page == 0
#False:word_per_page == int(input("Number of words per page:"))
#Fix the code
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)
#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)
"""
Inside the most debuggers,you can put a breakpoint which
tells the computer to stop what you're doing at this particular line.
And then at that moment in time,I want to examine what all the variables
and all the functions are doing.
"""
#Fix the code
def mutate(a_list):
b_list = []
for item in a_list:
new_item = item * 2
b_list.append(new_item)
print(b_list)
#7.Take a Break
#8.Ask a Friend
#9.Run Often(run your code often)
"""
Don't wait until you've written loads and loads of code to hit run,
and then find out you've got loads of snags and loads of bugs.
"""
#10.Ask StackOverFlow
#13.1 Debug Odd or Even Exercise
number = int(input("Which number do you want to check?"))
if number % 2 == 0:#SyntaxError:invalid syntax
print("This is an even number.")
else:
print("THis is an odd number.")
#two equal signs is checking and one equal sign is assignment
#13.2 Debug Leap Year Exercise
year = int(input("Which year do you want to check?"))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print("Leap Year.")
else:
print("Not leap year.")
else:
print("Leap year")
else:
print("Not leap year")
"""
TypeError:not all arguments converted during string formatting
type(year):class 'str'
type(4):class 'int'
"""
#Debug Fizz Buzz Exercise
for number in range(1,101):
if number % 3 == 0 and number % 5 == 0:
print("FizzBuzz")
elif number % 3 == 0:
print("Fizz")
elif number % 5 == 0:
print("Buzz")
else:
print(number)
day13_Udemy Python 100days
最新推荐文章于 2024-08-19 14:41:29 发布