通过短暂的基础学习之后,终于到了Python中面向对象的方式来写代码。
如何随机将整数生成类,可以指定一批生成的个数,可以指定数字的范围,可以调整每批生成数字的个数。接着接着讲坐标打印出来,要使用上题中的类,随机生成20个数字,将他们凉凉配对成二维坐标系的坐标,并把这些坐标组织起来打印出来。
1.随机生成类
import random
class Num_Int:
def init(self,count,stare,end):
self.count = count
self.stare = stare
self.end = end
self.names = []
def choice(self):
for i in range(self.count):
self.names.append(random.randint(self.stare,self.end))
return self.names
r=Num_Int(100,1,10)
print(r.choice())
注:上图片中的代码请自行忽略。
2,坐标
import random
class Num_Int:
def init(self,count,stare,end):
self.count = count
self.stare = stare
self.end = end
self.names = []
def choice(self):
for i in range(self.count):
self.names.append(random.randint(self.stare,self.end))
return self.names
r=Num_Int(10,1,20)
print(r.choice())
class method:
def init(self,Num_Int):
self.Num_Int = Num_Int
def point(self):
# nums = [12, 13, 17, 13, 1, 11, 9, 9, 1, 2]
nums = self.Num_Int.choice()
length = len(nums)
for i in range(0, length, 2):
print((nums[i], nums[i + 1]))
mm = method(r)
mm.point()