try: a = int(input("输入被除数:")) b = int(input("输入除数:")) c = a / b print("您输入的两个数相除的结果是:", c ) except (ValueError, ArithmeticError): print("程序发生了数字格式异常、算术异常之一") except : print("未知异常") print("程序继续运行") ================================================== 结果: 输入被除数:a 程序发生了数字格式异常、算术异常之一 程序继续运行
获取特定异常有关信息
args:返回异常的错误编号和描述字符串;
str(e):返回异常信息,但不包括异常信息的类型;
repr(e):返回较全的异常信息,包括异常信息的类型。
举例:
1 2 3 4 5 6 7 8 9 10 11
try: 1/0 except Exception as e: # 访问异常的错误编号和详细信息 print(e.args) print(str(e)) print(repr(e)) ================================= ('division by zero',) division by zero ZeroDivisionError('division by zero',)
生成一个指定范围内的随机浮点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成的随机数n: b <= n <= a。如果 a <b, 则 a <= n <= b。
生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b,注意: 下限必须小于上限。
从指定范围内,按指定基数递增的集合中 获取一个随机数。
从序列中获取一个随机元素。
将一个列表中的元素打乱.
从指定序列中随机获取指定长度的片断,不会修改原有序列。
入门Seaborn库
官网介绍
Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics.
For a brief introduction to the ideas behind the library, you can read the introductory notes or the paper. Visit the installation page to see how you can download the package and get started with it. You can browse the example gallery to see some of the things that you can do with seaborn, and then check out the tutorial or API reference to find out how.
To see the code or report a bug, please visit the GitHub repository. General support questions are most at home on stackoverflow or discourse, which have dedicated channels for seaborn.
from ast import Try from multiprocessing.heap import Arena import numpy as np import random import seaborn as sbn import matplotlib.pyplot as plt
defForest_Fire(current_matrix,p,f): matrix = current_matrix # (1)空位生长树木 (0 --> 1) 储存位置 i_C_indexes = [] j_C_indexes = [] for i inrange(area): for j inrange(area): if matrix[i,j] == 0and random.random<p: i_C_indexes.append(i) j_C_indexes.append(j) else: pass # (2)周围树木燃烧 (1 --> -1) 储存位置,并存储上一时刻的燃烧数据 fire_memory = np.where(matrix==-1) i_indexes = [] j_indexes = [] for i inrange(area): for j inrange(area): if matrix[i,j] == -1: try: if matrix[i-1,j] == 1: i_indexes.append(i-1) j_indexes.append(j) except: pass try: if matrix[i+1,j] == 1: i_indexes.append(i+1) j_indexes.append(j) except: pass try: if matrix[i,j-1] == 1: i_indexes.append(i) j_indexes.append(j-1) except: pass try: if matrix[i,j+1] == 1: i_indexes.append(i) j_indexes.append(j+1) except: pass else: pass for k inrange(len(i_indexes)): matrix[i_indexes[k],j_indexes[k]] = -1
# (3)燃烧树木清除 (-1 --> 0) matrix[fire_memory] = 0 # (4)雷电击中正常树木 (1 --> -1) 储存位置 i_indexes = [] j_indexes = [] for i inrange(area): for j inrange(area): if matrix[i,j] == 1: try: if matrix[i-1,j] == -1: continue else: pass except: pass
try: if matrix[i+1,j] == -1: continue else: pass except: pass
try: if matrix[i,j-1] == -1: continue else: pass except: pass
try: if matrix[i,j+1] == -1: continue else: pass except: pass
if random.random() < f: i_indexes.append(i) j_indexes.append(j) else: pass else: pass for k inrange(len(i_indexes)): matrix[i_indexes[k],j_indexes[k]] = -1 # (5)完成空位生长 for k inrange(len(i_indexes)): matrix[i_indexes[k],j_indexes[k]] = 1
return matrix
defmain(area,N,p,f): matrix = np.ones([area,area]).astype("int") #N次模拟火灾 for time inrange(N): matrix = Forest_Fire(matrix,p,f) C_count.append(len(np.where(matrix==0)[0])) G_count.append(len(np.where(matrix==1)[0])) R_count.append(len(np.where(matrix==-1)[0]))