day13_Udemy Python 100days

    #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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值