给程序传参数
import sys
print(sys.argv)
name = sys.argv
print("热烈欢迎%s的到来"%name)
python3 sys.py jalynnxi
打印结果:
'['sys.py', 'jalynnxi']
热烈欢迎jalynnxi的到来
#python2
>>> range(10,20)
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> range(10,18,3)
[10, 13, 16]
#python3
>>> range(10,20)
range(10, 20)
列表生成式
在用时再生成,以免浪费太多的内存。如下把range的列表里需要的信息取出来存入a,b
>>> a = [i for i in range(1,18)]
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
>>> b = [11 for i in range(1,18)]
>>> b
[11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11]
>>> c = [i for i in range(10) if i%2 ==0]
>>> c
[0, 2, 4, 6, 8]
>>> d = [i for i in range(3) for j in range(2)]
>>> d
[0, 0, 1, 1, 2, 2]
>>> e = [(i,j) for i in range(3) for j in range(2)]
>>> e
[(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]
#####集合
#集合里的数据不可重复,用大括号定义的除了字典,还有集合
>>> c = {11,22,33,11,22,33}
>>> c
{33, 11, 22}
给列表去重
>>> c = {11,22,33,11,22,33}
>>> c
{33, 11, 22}
>>> a = [11,22,33,11,22,33]
>>> f = set(a)
>>> f
{33, 11, 22}
>>> b = list(f)
>>> b
[33, 11, 22]
集合也可以增删改查,但是如果增加的元素之前集合里就有,则添加失败
练习
老王开枪
class Person(object):
def __init__(self,name):
super(Person,self).__init__()
self.name = name
self.gun = None
self.hp = 100
def anzhuang_zidan(self,danjia_temp,zidan_temp):
danjia_temp.baocun_zidan(zidan_temp)
def anzhuang_danjia(self,gun_temp,danjia_temp):
gun_temp.baocun_danjia(danjia_temp)
def naqiang(self,gun_temp):
self.gun = gun_temp
def koubanji(self,diren):
self.gun.fire(diren)
def diaoxue(self,shashangli):
self.hp -= shashangli
def __str__(self):
if self.gun:
return "%s的血量为%d,他有枪%s"%(self.name,self.hp,self.gun)
else:
if self.hp>0:
return "%s的血量为%d,他没有枪"%(self.name,self.hp)
else:
return "敌人已经被打死"
class Gun(object):
def __init__(self,name):
super(Gun,self).__init__()
self.name = name
self.danjia = None
def baocun_danjia(self,danjia_temp):
self.danjia = danjia_temp
def fire(self,diren):
zidan_temp = self.danjia.tanchu_zidan()
if zidan_temp:
zidan_temp.dazhong(diren)
else:
print("没有子弹了")
def __str__(self):
if self.danjia:
return "枪的信息为:%s,弹夹的信息为:%s"%(self.name,self.danjia)
else:
return "枪的信息为:%s,这把枪没有弹夹"%(self.name)
class Danjia(object):
def __init__(self,max):
super(Danjia,self).__init__()
self.max = max
self.zidan_list = []
def baocun_zidan(self,zidan_temp):
self.zidan_list.append(zidan_temp)
def tanchu_zidan(self):
if self.zidan_list:
return self.zidan_list.pop()
else:
None
def __str__(self):
return "弹夹的信息为%d/%s"%(len(self.zidan_list),self.max)
class Zidan(object):
def __init__(self,shashangli):
super(Zidan,self).__init__()
self.shashangli = shashangli
def dazhong(self,diren):
diren.diaoxue(self.shashangli)
def main():
#用来控制整个程序流程
#1、创建老王对象
laowang = Person("老王")
#2、创建枪对象
ak47 = Gun("AK47")
#3、创建弹夹对象
danjia = Danjia(20)
#4、创建子弹对象
for i in range(15):
zidan = Zidan(10)
laowang.anzhuang_zidan(danjia,zidan)
#5、创建敌人
diren = Person("敌人")
print(diren)
#6、老王把子弹安装到弹夹中
# laowang.anzhuang_zidan(danjia,zidan)
#7、老王把弹夹装到抢中
laowang.anzhuang_danjia(ak47,danjia)
# print(danjia)
# print(ak47)
#8、老王拿枪
laowang.naqiang(ak47)
print(laowang)
#9、老王开枪打敌人
laowang.koubanji(diren)
print(diren)
if __name__ == '__main__':
main()
运行结果:
敌人的血量为100,他没有枪
老王的血量为100,他有枪枪的信息为:AK47,弹夹的信息为:弹夹的信息为15/20
敌人的血量为90,他没有枪
敌人的血量为80,他没有枪
网友评论