Python遇到的一些小问题

一、Python代码延迟等待:

导入time模块,sleep中参数单位为秒

import time

print(“hello”)
time.sleep(1)
print(“world”)

二、printh换行:

默认print()自动换行

print("hello",end='')

加入end后就不换行

三、创建固定大小一维数组、二维数组

1、创建一维数组:
创建长度为5,值为‘’的一维数组

list=['']*5

2、创建二维数组
可以用numpy
创建5*5矩阵

import numpy as np
np.zeros((5,5))

四、报错

1、Python报错:UnboundLocalError: local variable x referenced before assignment
一般是变量引用时出现的问题,可以将全局变量作为参数传入函数中,也可以在使用变量前加上global声明。

#作为参数传入
def f(n):
	f+=1

#声明global
def f:
	global n
	n+=1

2、Python报错:ValueError: ‘c’ argument has 5 elements, which is inconsistent with ‘x’ and ‘y’ with size 4.
出现在绘图时,一般是因为color序列与节点个数不相符导致

3、Python报错:AttributeError: module ‘tkinter’ has no attribute 'filedialog’
出现在文件对话框调用,from tkinter import filedialog把filedialog模块调入即可

猜你喜欢

转载自blog.csdn.net/m0_47470899/article/details/114002070