首先是第一个py文件


#coding:gbk

from random import choice

class randomwalk():
	'''生产一个随机漫步数据类'''
	
	def __init__(self,numpoints=5000):
		'''初始化随机漫步的属性'''
		self.numpoints = numpoints
		'''所有随机漫步都始于(0,0)'''
		self.xvalues = [0]
		self.yvalues = [0]
	
	def fill_walk(self):
		'''计算机随机漫步包含的所有点'''
		#不断漫步,知道列表达到指定长度
		while len(self.xvalues) < self.numpoints:
			#决定前进方向以及沿着这个方向前进的距离
			
			x_direction = choice([1,-1])
			x_distance = choice([0,1,2,3,4])
			x_step = x_direction * x_distance
			
			y_direction = choice([1,-1])
			y_distance = choice([0,1,2,3,4])
			y_step = y_direction * y_distance
			
			#拒绝原地踏步
			if x_step == 0 and y_step == 0:
				continue
			
			#计算下一个点的X和Y值
			next_x = self.xvalues[-1] + x_step
			next_y = self.yvalues[-1] + y_step
			
			self.xvalues.append(next_x)
			self.yvalues.append(next_y)
			

#问题卡在下一个py文件.


#coding:gbk

import matplotlib.pyplot as plt

from radom_walk import randomwalk


#创建一个random实例,并将其包含的点都绘制出来
while True:
	'''只要程序处于活动状态,就不断地模拟随机漫步'''
	rw =randomwalk()
	rw.fill_walk()
	
	pointnums = list(range(rw.numpoints)#这里记得加个右括号
	plt.scatter(rw.xvalues,rw.yvalues,c=pointnums,cmap=plt.cm.Blues,edgecolor,s=15)
	plt.show()

	keepruning = input('你想再次生成一个随机漫步图吗?(y/n) :')
	if keepruning == 'n':
		break

plt.scatter(rw.xvalues,rw.yvalues,c=pointnums,cmap=plt.cm.Blues,edgecolor,s=15)


SyntaxError: invalid syntax



 

 

最后修改:2022 年 12 月 05 日
如果觉得我的文章对你有用,请随意赞赏