-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.py
More file actions
27 lines (24 loc) · 760 Bytes
/
class.py
File metadata and controls
27 lines (24 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# ************************************************************
# Author - Harshit Prasad
# 18.12.2016
# Python Program - Classes and Objects.
# ************************************************************
class Enemy:
life = 3 # defining a variable in a class.
def attack(self):
print('ouch!')
self.life -= 1
def checklife(self):
if self.life <=0:
print("'You're dead!")
else:
print(self.life, 'life left.')
enemy1 = Enemy()
enemy2 = Enemy()
enemy1.attack()
enemy1.attack()
# enemy 1 has been attacked two times.
enemy2.checklife()
enemy1.checklife()
# Here since enemy2 is not attacked, his life will be equal to 3.
# Whereas, enemy1 has been attacked 2 times. So, his life will become 1.