forked from wzpan/Learn-Python-The-Hard-Way
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex40.py
More file actions
executable file
·70 lines (50 loc) · 1.21 KB
/
ex40.py
File metadata and controls
executable file
·70 lines (50 loc) · 1.21 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
#!/bin/python2
# -*- coding: utf-8 -*-
# ex40: Modules, Classes, and Objects
# a first class example
class Song(object):
def __init__(self, disk):
self.index = 0
self.disk = disk
self.jump()
def next(self):
''' next song. '''
self.index = (self.index + 1) % len(self.disk)
self.jump()
def prev(self):
''' prev song. '''
self.index = (self.index - 1) % len(self.disk)
self.jump()
def jump(self):
''' jump to the song. '''
self.lyrics = self.disk[self.index]
def sing_me_a_song(self):
for line in self.lyrics:
print line
# construct a disk
song1 = ["Happy birthday to you",
"I don't want to get sued",
"So I'll stop right there"]
song2 = ["They rally around the family",
"With pockets full of shells"
]
song3 = ["Never mind I find",
"Some one like you"
]
disk = [song1, song2, song3]
mycd = Song(disk)
mycd.sing_me_a_song()
mycd.next()
mycd.sing_me_a_song()
mycd.next()
mycd.sing_me_a_song()
mycd.next()
mycd.sing_me_a_song()
mycd.prev()
mycd.sing_me_a_song()
mycd.prev()
mycd.sing_me_a_song()
mycd.prev()
mycd.sing_me_a_song()
mycd.prev()
mycd.sing_me_a_song()