Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsuperhero.py
More file actions
Latest commit
91 lines (69 loc) · 2.46 KB
/
Copy pathsuperhero.py
File metadata and controls
91 lines (69 loc) · 2.46 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
This module provides a set of classes for creating superheros
and supervillains. Have fun!
Author - Christopher Woods
License - BSD
"""
classSuperhero:
"""This class allows you to create your own Superhero"""
def__init__(self, name, weakness):
"""Construct a superhero with the specified name and the
specified weakness
"""
self.setName(name)
self.setWeakness(weakness)
defsetName(self, name):
"""Set the name of the superhero"""
self._name=name
defsetWeakness(self, weakness):
"""Set the weakness of the superhero"""
self._weakness=weakness
defgetName(self):
"""Return the name of the superhero"""
returnself._name
defgetWeakness(self):
"""Return the weakness of the superhero"""
returnself._weakness
defisVulnerableTo(self, item):
"""Return whether or not this superhero is
vulnerable to 'item'"""
returnself.getWeakness().lower() ==item.lower()
classSupervillain:
"""This class allows you to create your own supervillain"""
def__init__(self, name):
self.setName(name)
self._loot= []
defsetName(self, name):
"""Set the name of the villain"""
self._name=name
defgetName(self):
"""Return the name of the villain"""
returnself._name
defsteal(self, item):
"""Tell the villain to steal 'item'"""
self._loot.append(item)
defgetLoot(self):
"""Return all of the loot that this villain has stolen"""
returnself._loot
defbattle(superhero, supervillain):
"""This function will pitch the superhero and villain
into battle, and will return the name of whoever wins!
"""
try:
forpossessioninsupervillain.getLoot():
ifsuperhero.isVulnerableTo(possession):
returnsupervillain.getName()
returnsuperhero.getName()
exceptExceptionase:
# Draw, so no-one won!
return"No-one, because %s"%e
superman=Superhero(name="Superman", weakness="kryptonite")
print("Is it a bird? Is it a plane? No, it's %s!!!"%superman.getName())
lex=Supervillain(name="Lex Luther")
print("%s will battle %s. The winner is %s" \
% (superman.getName(), lex.getName(), \
battle(superman, lex) ) )
print("Lex steals some krytonite...")
lex.steal("kryptonite")
print("They battle again... The winner is %s" \
%battle(superman, lex))