-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP_1.py
More file actions
52 lines (42 loc) · 1.64 KB
/
OOP_1.py
File metadata and controls
52 lines (42 loc) · 1.64 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Person:
def __init__(self, name, money):
self.name = name
self.money = money
def introduce(self):
print(self.name + ' has got ' + str(self.money) + ' dollars now')
return self.name, self.money
def share(self, somebody, money):
if self.money/2 > money:
self.money -= money
somebody.money += money
print(self.name + ' lent ' + somebody.name, str(money) + " dollars. Now " + self.name + "'s got " + str(self.money) + ' and ' + somebody.name + "'s got " + str(somebody.money) + " dollars")
else:
print(somebody.name, 'asked', self.name, 'for', str(money),'dollars, but', self.name, 'cold not lend that much')
return self.money, somebody.money
class Robber:
def __init__(self, money, tool):
self.money = money
self.tool = tool
def needs(self):
print('A person with a ' + self.tool + ' needs ' + str(self.money) + ' dollars')
return self.money, self.tool
def rob(self, somebody):
if somebody.money > self.money:
somebody.money -= self.money
print('A person with a ' + self.tool + ' stole ' + str(self.money) + " dollars from " + somebody.name)
self.money = 0
else:
self.money -= somebody.money
somebody.money = 0
print('A person with a ' + self.tool + ' stole all the money from ' + somebody.name)
return somebody.introduce(), self.needs()
Mike = Person('Mike', 100)
Lisa = Person('Lisa', 400)
Luke = Robber(300, 'gun')
Mike.introduce()
Lisa.introduce()
Luke.needs()
print()
Luke.rob(Mike)
print()
Luke.rob(Lisa)