首页 > 编程语言 >Python3

Python3

时间:2023-11-15 14:23:52浏览次数:34  
标签:do code except your error check Python3

import numpy as np
x = np.array([1.0, 2.0, 3.0, 4.0])
y = x + 2
Ans:
array([3., 4., 5., 6.])
y = x > 2.0
Ans:
array([False, False, True, True])

 

A. SyntaxError - also known as parsing error. Python does not like your structure.
It is like Dr F asks you to redo your essay because of your format is wrong or missing dates.
Examples: 
    Missing ’:’, ’()’ etc. 
Displays a little ‘arrow’ pointing at the earliest point in the line where the error was detected but maybe not be where the error is!
B. Exceptions - errors found during running
A NameError - name of function or variables notfound 
B TypeError - operations on different data types
C.ZeroDivisionError
D.FileNotFoundError
E.IndexError - wrong index used 
F. KeyError - in dictionary
G IndentationError - wrong or no indent 
H .... More
This is like Dr F accepted your essay format and started reading it but found a lot of weird problems. So, he asked you to correct your essay.
Part I:
A. SyntaxError
— check missing colons or parentheses
B. NameError
— check for typos, function definitions
C. TypeError
— check variable types
D. ValueError
— check function arguments
E. FileNotFoundError
— check that files exist

Part II:
A. IndexError
— check for nonexistent list elements (sometimes python allows and does show this error)
B. KeyError
— similar to an IndexError, but for dictionaries
C. ZeroDivisionError
D. IndentationError
— check the number of spaces and tabs infront of the commands are not mixed

 AssertionError

'2'+2 TypeError

Most of the time, we do not want errors to happen.
But it happens! Next best thing is we do not want our 
program to crash (stop executing)!
We can tell Python to try a block of code, and it will run 
normally except if something goes wrong.
# calculate square roots 
d = list( range( 10 ) ) 
r = []
for i in d: 
    try:
        r[ i ] = sqrt( d[ i ] ) 
    except:
        print( ’An error occurred.’ ) 
        break    

try:
    # the main code
    # if an error occurs, it goes into ”except:”
    immediately
except:
    # an error occurs
else: (optional)
    # if no error occurs
finally: (optional)
    # this always happens, error or no error
Note: Using except: or except XXXError: both will work.
XXXError is from the list of errors/exceptions in Python (like 
those at the first few slides in this lecture)

 

 

 

 

 

 

D. Debugging strategies
A. Start early.
B. Read the problem statement carefully.
C. Write down on paper what you need to do STEP-BYSTEP.
D. Add print statements.
E. Break the program down into parts (e.g., write functions).
F. Document functions before writing them. (# Put comments 
to remind yourself what you are trying to do)
G. Explain it to someone else.
H. Make no assumptions! If your thinking is not precise, your 
code will not be precise.
I. Start over from scratch. Take a fresh look at the problem.
Style
Why do we write comments?
For the person who next looks at the code! Also for YOU 
after you wrote the code a long time ago!
x_vals = [0,0.1,0.2,0.3,0.4] # in meters

Use descriptive variable names. 
pi = 3.141

 0.3用浮点数表示……

 0.010->0.01

0.011->0.10

标签:do,code,except,your,error,check,Python3
From: https://www.cnblogs.com/latent-Lin/p/17833717.html

相关文章

  • 2023最新!Python3.12于win10环境下的安装配置
    2023最新!Python3.12于win10环境下的安装配置资源:Python官网,Python3.12.0此为我记录Python安装,参考了这位博主的文章https://blog.csdn.net/thefg/article/details/128601410导航目录2023最新!Python3.12于win10环境下的安装配置导航一、下载Python二、安装Python三、检验是否安......
  • centos8 python3 安装达梦驱动 dmpython
    方案一全新安装:1.https://eco.dameng.com/download/下载dm8_20231011_x86_rh6_64.iso到/aaa目录2.创建用户所在的组groupadddinstall3.创建用户useradd-gdinstall-m-d/home/dmdba-s/bin/bashdmdba4.修改用户密码passwddmdba5.修改文件打开最大数vi/etc/sec......
  • [-007-]-Python3+Unittest+Selenium Web UI自动化测试之等待
    selenium中,经常会出现元素还没有加载出来,浏览器找不到元素而报错的问题,设置等待是保证脚本运行的一个重要手段,常用的等待有三种--强制等待、隐式等待、显示等待。1.强制等待time.sleep(10)必须等待10s,不太实用2.隐式等待driver.implicitly_wait(最大等待时间X秒)设置一次,则......
  • python3使用pymsql操作mysql数据库
    操作系统:Windows10_x64python版本:3.9.2pymysql版本:1.0.2MySQL版本:5.7.38 之前写过一篇关于python操作mysql数据库的文章:https://www.cnblogs.com/MikeZhang/p/pythonOptMysql20170703.html当时是基于python2.7和mysql5.5来整理的,但目前python2.7已经不再维护,主......
  • python3: dlt - 数据结构2
    python3:dlt-数据结构2    一、源程序1[wit@fedoranull]$cattest.py2#!/usr/bin/envpython334567#file_name=test.py8#python_verion=3.11.1910111213#testthisscript14defmsg():15print......
  • python3: dlt - 数据结构
    python3:dlt-数据结构    一、程序:1[wit@fedoranull]$cattest.py2#!/usr/bin/envpython334567#testthisscript8defmsg():9print("\nhello,python3!\n")101112#runningmsg()13#msg()1415......
  • [-006-]-Python3+Unittest+Selenium Web UI自动化测试之悬浮窗口中的元素点击
     1.分析现状:PPT模板悬浮出现悬浮窗口悬浮窗口中分为4大类:PPT模板,PPT模板页,PPT关系图,PPT图表大类下存在小类点击可跳转但是此页面里还存在PPT模板下的总结汇报等此种情况的元素此情况如果仅用text定位是无法定位到的所以排除了text定位方式2.解决方法:首先我们看下悬浮窗......
  • python3-TK实现一个可视化界面,选中文件夹可以计算文件夹下文件的数量
    借助Python3中Tkinter库,实现一个可视化的界面,通过界面选择文件夹,可以计算文件夹下文件的数量,嵌套文件夹的情况依旧可以计算。importosimporttkinterastkfromtkinterimportfiledialogdefcount_files_in_folder(folder_path):file_count=0forroot,dirs,......
  • python3使用sqlite3构建本地持久化缓存
    环境:Windows10_x64python版本:3.9.2sqlite3版本:3.34.0日常python开发中会遇到数据持久化的问题,今天记录下如何使用sqlite3进行数据持久化,并提供示例代码及数据查看工具。一、背景描述python应用程序在运行过程中被kill掉(比如版本升级等情况),内存中的运行数据将会丢失,如果能够......
  • python3
    循环 1"""2while条件:3满足条件4#只要条件满足,会无限循环执行5"""6i=07sum=08whilei<=100:9sum=sum+i10i+=111print(sum)while循环猜数字1#无限次猜数字2importrandom3num=random.randint(1,100)......