Home > other >  Python
Python

Time:09-20

1. The soldiers have a saying-more than three AK47
2. The soldiers can fire
3. The gun to shoot
4. Gun loading reload - to increase the number of bullets
What written in Python

CodePudding user response:

The class gnu:

Def __init__ (self, name) :
The self. The name=name
The self. The count=0

Def launch (self) :
If the self. The count==0:
Print (" % no bullets to shoot "% self. The name)
Return
The self. The count -=1
Bullets, print (" % s and % d bullet "% (self. The name, the self. The count))

Def add_bullet (self, count) :
The self. The count +=count
Increase the print (" % s % d bullet, now there are % d bullet "% (self. The name, the count, the self. The count))


The class Soilder:

Def __init__ (self, name) :
The self. The name=name
The self. The gnu=None

Def the fire (self) :
If self. Gnu is None:
Don't have a gun print (" % s "% self. The name)
Return

The self. The gnu. Add_bullet (30)

The self. The gun. The launch ()

# 1. Soldiers have a saying-more than three AK47
Xusanduo=Soilder (" xu sanduo ")
Xusanduo. Gnu=gnu (" AK47 ")
# 2. Soldiers can fire
Xusanduo. The fire ()
# 3. The gun to shoot
# 4. Gun reload - to increase the number of bullets