当前位置:网站首页>【bug 简单处理】
【bug 简单处理】
2022-07-23 01:22:00 【lxw-pro】
bug 简单处理目录
个人昵称:lxw-pro
个人主页:欢迎关注 我的主页
个人感悟: “失败乃成功之母”,这是不变的道理,在失败中总结,在失败中成长,才能成为IT界的一代宗师。
bug是程序错误的统称。
Python江湖中,将整治bug称为debug
- “思路不清bug”是初学者最常见的bug,解决了它,就解决了大部分bug。
- 思路不清bug主要是由于我们在面对问题时, 由于我们对问题及细节思考不到位,导致”
一招不慎,全盘报错”。
有的时候还不报错,但就达不到我们想要的效果。
注释我们也使用过。 如果某行或某段代码写的总是不对,你可以使用注释将当前代码注释掉, 再一步一步运行,排除错误。注:print()函数与#注释搭配起来更好用。
film = {
'星汉灿烂': ['吴磊', '赵露思'],
'特种兵之女凤凰': ['徐佳', '刘晓洁'],
'特战荣耀': ['杨洋', '李一桐'],
'铤而走险': ['大鹏', '李梦', '欧豪'],
'使徒行者': ['张家辉', '古天乐']
}
print(film)
star = input('你想看哪位演员的电影?')
for i in film:
actors = film[i]
# print(actors)
if star in actors:
print(star+'出演影片'+i)

思路不清
被动掉坑
有时候代码没问题, 而是用户操作不正确,导致程序出现问题。
敲代码的时候我们难免会碰到一些bug即便是技术大牛也是如此,是必不可少的,也是我们成长路上的最好的佐料,要有足够的耐心不断解决不断积累,躲过了这些坑,相信自己也是把解bug老手
bug: append一次只能加一个
list = []
list.append('X')
list.append('Y')
list.append('Z')
print(list)
[^1] debug:一次依次加一个
要是一起加进去,程序就会报错!!!
异常处理
在Python江湖中,Python给我们提供了一种异常处理机制,用来内部消化出现的异常,让程序继续执行。
- 不知道用户什么时候会输入正确,什么时候会输入错误, 设置
while循环来接收输入,
只要用户输入的不是数字就一直循环,用户输入数字后用break跳出循环。 - 使用
try…except…异常捕获机制, 用户输入不正确时就会一直提示。
while True:
try:
age = int(input('你今年多大了?'))
break
except ValueError:
print('你输入的不是数字')
if age < 18:
print('不可以抽烟喝酒烫头哦')
print('你已经不是小孩啦!')
像这个虽然当中有被除数为0的,不过程序就不会报错,只会很正常地提示你,哪儿有误!try…except…果然好用,赞一个!
while True:
try:
num = [5, 6, 0, 10]
for i in num:
print(600 / i)
except:
print("被除数不能为0")
break
异常是在程序运行过程中发生的错误,当异常发生时,需要对异常进行处理,否则整个程序将崩溃
try-except:
try 和 except 语句块可以用来捕获和处理异常,
try 后面跟的是需要捕获异常的代码,except 后面跟的是捕获到异常后需要做的处理。
每一个 try 语句块后面必须跟上一个 except 语句块,即使 except 语句块什么也不做。
try:
print(1/0)
# 除0异常
with open('test.log') as file:
read_data=file.read()
# 文件不存在异常
except ZeroDivisionError:
print("ZeroDivisionError happened!")
except FileNotFoundError:
print("FileNotFoundError happened!")
print("Done!")
try-except-else:
try-except 语句块后面可以跟上 else 语句块,
当没有异常发生时,会执行 else 语句块中的代码
try:
print(1/1)
except ZeroDivisionError:
print("ZeroDivisionError happened!")
else:
print("Exception not happened")
print("Done!!")
try-except-else:
try-except-else 语句块后面还可以跟上 finally 语句块,
不管有没有发生异常,finally 语句块中的代码都会被执行。
try:
print(1/0)
# print(2/1)
except ZeroDivisionError:
print("ZeroDivisionError happened!")
else:
print("Exception not happened")
finally:
print("Finally is executed!")
print("Done!!!")
finally 在释放资源时会特别有用
注:主动抛出异常。(主动抛出异常使用 raise 关键字)
————————————————————————————————————————————
pandas 每日一练:
注:print()只为转行
# -*- coding = utf-8 -*-
# @Time : 2022/7/21 20:25
# @Author : lxw_pro
# @File : pandas-4 练习.py
# @Software : PyCharm
import pandas as pd
import numpy as np
lxw4 = {"project": ['Python', 'Java', 'C', 'MySQL', 'Linux', 'Math', 'English', 'Python'],
"popularity": [91, 88, 142, 136, np.nan, 146, 143, 148]}
df = pd.DataFrame(lxw4)
16、查看最后5行数据
zh = df.tail()
print("查看最后5行数据为:\n", zh)
print()
17、删除最后一行数据
df.drop([len(df)-1], inplace=True)
print("删除最后一行数据的结果为:\n", df)
print()
18、添加一行数据[‘go语言’, 66]
row = {'project': 'go语言', 'popularity': 66}
df = df.append(row, ignore_index=True)
print("添加一行数据['go语言', 6.6]的结果为:\n", df)
print()
19、对数据按照“popularity”列值得大小进行排序
df.sort_values('popularity', inplace=True)
print("排序后的结果为:\n", df)
print()
20、统计project列每个字符串的长度
df['project'] = df['project'].fillna('R')
df['str_len'] = df['project'].map(lambda x: len(x))
print("统计project列每个字符串的长度为:\n", df)
相关运行结果如下:
16~17:

18~19:
20:
每日一言:
我还有许多弯路要走,还会失望于许许多多的满足。一切都要等日后才能显示它的意义!
在人生的道路上,当你的希望一个个落空的时候,你也要坚定,要沉着!!
持续更新中…
点赞,你的认可是我创作的
动力!
收藏,你的青睐是我努力的方向!
评论,你的意见是我进步的财富!
关注,你的喜欢是我长久的坚持!
欢迎关注微信公众号【程序人生6】,一起探讨学习哦!!!
边栏推荐
- 教育机器人对学生学习效果的实际影响
- 【管理篇 / 升级】* 02. 查看升级路径 * FortiGate 防火墙
- BCG 使用之NOTIFYICONDATA托盘
- -Bash: wget: command not found
- Online matting and background changing and erasing tools
- Huawei applications have called the checkappupdate interface. Why is there no prompt for version update in the application
- 如何高效系统学习 MySQL?
- codeforces每日5题(均1500)-第二十三天
- PyG利用MessagePassing搭建GCN实现节点分类
- 在通达信开户安全不
猜你喜欢

Wallys/DR4019S/IPQ4019/11ABGN/802.11AC/high power

-bash: wget: 未找到命令

【MySQL从入门到精通】【高级篇】(七)设计一个索引&InnoDB中的索引方案

Event listening and deleting events - event object - default event - cancel bubbling event - event delegation - default trigger

读书笔记:程序员的自我修养---第三章

事件侦听和删除事件——event对象——默认事件——取消冒泡事件——事件委托——默认触发

Swin transformer object detection project installation tutorial

免费屏幕录像机

Avantages de la salle des machines bgp

Solve the greatest common divisor and the least common multiple
随机推荐
【FPGA教程案例36】通信案例6——基于vivado核的FFT傅里叶变换开发以及verilog输入时序配置详解,通过matlab进行辅助验证
【MySQL从入门到精通】【高级篇】(七)设计一个索引&InnoDB中的索引方案
模板学堂丨JumpServer安全运维审计大屏
1646. 获取生成数组中的最大值递归法
Cbcgpcolordialog control used by BCG
在通达信开户安全不
Swin transformer object detection project installation tutorial
Props and context in setup
PyG利用MessagePassing搭建GCN实现节点分类
【CANN训练营】学习笔记——Diffusion和GAN对比,Dalle2和Parti
It is not safe to open an account in tongdaxin
La fosse Piétinée par l'homme vous dit d'éviter les 10 erreurs courantes dans les tests automatisés
一个月学透阿里整理的分布式架构笔记
力扣(LeetCode)203. 移除链表元素(2022.07.22)
微信小程序设置背景图片不显示问题解决方法
华为应用已经调用了checkAppUpdate接口,为什么应用内不提示版本更新
C#之winform窗体的最大化、最小化、还原、关闭以及窗体的移动
Ascension begins with change
银联最新测试工程师笔试题目,你能得多少分?
2302. Count the number of subarrays with a score less than k - sliding array - double hundred code
