-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.py
More file actions
47 lines (35 loc) · 940 Bytes
/
class.py
File metadata and controls
47 lines (35 loc) · 940 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Spaceship:
# Class attribute
tractor_beam = 'off'
# Instance attributes
def __init__(self, name, kind):
self.name = name
self.kind = kind
self.speed = None
# Instance methods
def warp(self, warp):
self.speed = warp
print(f'Warp {warp}, engage!')
def tractor(self):
if self.tractor_beam == 'off':
self.tractor_beam = 'on'
print('Tractor beam on.')
else:
self.tractor_beam = 'off'
print('Tractor beam off')
# Create an instance of the Spaceship class (i.e. "instantiate")
ship = Spaceship('Mockingbird','rescue frigate')
# Check ship's name
print(ship.name)
# Check what kind of ship it is
print(ship.kind)
# Check tractor beam status
print(ship.tractor_beam)
# Set warp speed
ship.warp(7)
# Check speed
print(ship.speed)
# Toggle tractor beam
ship.tractor()
# Check tractor beam status
print(ship.tractor_beam)