随机整数生成类,可以指定一批生成的个数,可以指定数值的范围,可以调整每批生成数字的个数
使用这个类,随机生成20个数字,两两配对形成二维坐标系的坐标,把这些坐标组织起来,并打印输出
一:
import random
class Randnum:
def __init__(self, rmin=0, rmax=100, num=10):
self.min = rmin
self.max = rmax
self.num = num
def creat(self):
return [random.randint(self.min, self.max) for x in range(self.num)]
test1 = Randnum()
print(test1.creat())
out:
[30, 18, 46, 63, 41, 31, 49, 93, 26, 9]
二:
import random
class Randnum:
@classmethod
def creat(cls, minnum=0, maxnum=100, num=10):
return [random.randint(minnum, maxnum) for x in range(num)]
print(Randnum.creat())
out:
[44, 82, 12, 48, 54, 36, 49, 98, 88, 96]
三:
import random
class RandomGenerator:
def __init__(self, count=10, start=1, stop=100):
self.count = count
self.start = start
self.stop = stop
self.gen = self._generate()
def _generate(self):
while True:
yield random.randint(self.start, self.stop)
def generate(self):
return [next(self.gen) for _ in range(self.count)]
rg = RandomGenerator(5)
print(rg.generate())
out:
[27, 79, 87, 29, 30]
完整:
import random
class RandomGenerator:
def __init__(self, count=10, start=1, stop=100):
self.count = count
self.start = start
self.stop = stop
self.gen = self._generate()
def _generate(self):
while True:
yield [random.randint(self.start, self.stop) for _ in range(self.count)]
def generate(self, count):
self.count = count
return next(self.gen)
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return '{}:{}'.format(self.x, self.y)
rg = RandomGenerator()
lst = [Point(x, y) for x, y in zip(rg.generate(10), rg.generate(10))]
# lst = [Point(*v) for v in zip(rg.generate(10), rg.generate(10))]
print(lst)
out:
[45:36, 1:36, 58:10, 58:20, 75:79, 12:66, 27:68, 98:26, 27:76, 100:67]
记录车的品牌Mark,颜色,价格,速度等特征,并实现增加车辆信息,显示全部车辆信息的功能
class Car:
def __init__(self, mark, color, price, speed):
self.mark = mark
self.color = color
self.price = price
self.speed = speed
class Carinfo:
def __init__(self):
self.lst = []
def addcar(self, car: Car):
self.lst.append(car)
def getall(self):
return self.lst[0].mark, self.lst[0].color, self.lst[0].price, self.lst[0].speed
ci = Carinfo()
car = Car('audi', '400', 'red', 100)
ci.addcar(car)
print(ci.getall())
out:
('audi', '400', 'red', 100)
实现温度的处理
实现华氏温度和摄氏温度的转换
℃ = 5 * (℉ - 32) / 9
℉ = 9 * ℃ / 5 + 32
模拟购物车
class Color:
RED = 0
BLUE = 1
GREEN = 2
GOLDEN = 3
BLACK = 4
OTHER = 1000
class Item:
def __init__(self, **kwargs):
self.__spec = kwargs
def __repr__(self):
return str(sorted(self.__spec.items()))
class Cart:
def __init__(self):
self.items = []
def additem(self, item:Item):
self.items.append(item)
def getallitems(self):
return self.items
mycart = Cart()
myphone = Item(mark='Huawei', color=Color.GOLDEN, memory='5G')
mycart.additem(myphone)
print(mycart.getallitems())
myphone = Item(mark='oppo', color=Color.BLACK, memory='5G')
mycart.additem(myphone)
print(mycart.getallitems())
Out:
[[('color', 3), ('mark', 'Huawei'), ('memory', '5G')]]
[[('color', 3), ('mark', 'Huawei'), ('memory', '5G')], [('color', 4), ('mark', 'oppo'), ('memory', '5G')]]