Python [failure] Debug Solver

debug # Solver failure

print () function normally and # annotation together to debug

Multi-line comments two shortcuts:
1, plus a group of three marks before and after the multi-line block requires comment '' '
2, after the selected code uses shortcut keys: Windows shortcut is ctrl + /, Mac is cmd + /, for local editor)

A mechanism for exception handling, you can instantly capture when an exception occurs, then the internal digest, let the program continue to run
try ... except ... statement

 

Python all error types
https://www.runoob.com/python/python-exceptions.html
############################ ################################################## ######
summary
bug caused by unfamiliar for knowledge, to remember to review, review notes, do the exercises targeted to grasp usage.

For thinking unclear bug, () function and comment # step by step to troubleshoot the error multi-purpose print.

For easy exceptions overlooked so passive out of the pit bug, try ... except statement can make the program run smoothly.

 

 


################################################## #################################
BaseException: contains all built-in exceptions
base class for all built abnormality
it should not directly inherit user-defined class

Exception: 不包含所有的built-in exceptions,
只包含built-in, non-system-exiting exceptions,
像SystemExit类型的exception就不包含在里面。

所有内置的非系统退出类异常都派生自此类。 所有用户自定义异常也应当派生自此类

###############################################################################
try...except语句的用法(可带多个)
try语句工作方式
1.执行try子句(在关键字try和关键字except之间的语句)

2.如果没有异常发生,忽略except子句,try子句执行后结束。

3.执行try子句异常,try子句余下的部分将被忽略
如果异常类型except 之后的名称相符,那么对应的except子句将被执行。
最后执行 try 语句之后的代码。

4.如果一个异常没有与任何的except匹配,那么这个异常将会传递给上层的try中 
##############################################################################
try except 语句还有一个可选的else子句
使用这个子句,那么必须放在所有的except子句之后。
这个子句将在try子句没有发生任何异常的时候执行

name = 0
try:
    print(name)
except NameError as a:
    print('------>',a)
else:
    print('在被检测代码块没有发生异常时执行')

优点:

else 子句比把所有的语句都放在 try 子句里面要好,
这样可以避免一些意想不到的、而except又没有捕获的异常

try:
  name
except NameError as a: #定制什么类型的异常 as
  print('------>',a)
#################################################################################
不管 try 子句里面有没有发生异常,finally 子句都会执行
如果一个异常在 try 子句里(或者在 except 和 else 子句里)被抛出,
而又没有任何的 except 把它截住,那么这个异常会在 finally 子句执行后被抛出。

try:
    d = {}
    d['name']
except Exception as e:
        print('---->', e)
else:
    print('在被检测代码块没有发生异常时执行')
finally:
    print('不管被检测的代码块有没有发生异常都会执行')
print('haha')

执行结果:
----> 'name'
不管被检测的代码块有没有发生异常都会执行
haha
###################################################################

import sys
def main():
    1/0
name = 0
try:
    print(name)
    main()
except NameError as a:
    print('------>',a)
except:                                     #最后一个except子句可以忽略异常的名称,它将被当作通配符使用
    print('Unexpected error:',sys.exc_info()[0])

 

列子:
print('\n欢迎使用除法计算器!\n')

while True:
    try:
        x = input('请你输入被除数:')
        y = input('请你输入除数:')
        z = float(x)/float(y)
        print(x,'/',y,'=',z)
        break  # 默认每次只计算一次,所以在这里写了 break。
    except ZeroDivisionError:  # 当除数为0时,跳出提示,重新输入。
        print('0是不能做除数的!')
    except ValueError:  # 当除数或被除数中有一个无法转换成浮点数时,跳出提示,重新输入。
        print('除数和被除数都应该是整值或浮点数!')

 

############################################################################################################################

# 方式2:将两个(或多个)异常放在一起,只要触发其中一个,就执行所包含的代码。
# except(ZeroDivisionError,ValueError):    #except子句可以同时处理多个异常,这些异常将被放在一个括号里成为一个元组
   #print('你的输入有误,请重新输入!')

# 方式3:常规错误的基类,假设不想提供很精细的提示,可以用这个语句响应常规错误。
# except Exception:               #except BaseException:
  # print('你的输入有误,请重新输入!')

Guess you like

Origin www.cnblogs.com/CH-TNT/p/11332518.html