Objective
If a class defines __iter__, __len__ and __contains__, its objects behave like collections: they are traversed with for, measured with len() and queried with in.
Instructions
- Define
Playlistwith an internal list of songs(title, seconds), a methodadd(title, seconds)and the special methods__len__,__iter__and__contains__(it searches by title). - Add
total_duration(), which adds the seconds of all the songs. - Read
nand thennlinestitle seconds, and finally a title to look for. - Go through the list with
forand show1. title (m:ss), with the duration as minutes and seconds (two digits for the seconds). - Show
Songs: n,Duration: m:ssandFound: Yes.
3
alba 185
mar 240
sol 65
mar
1. alba (3:05)
2. mar (4:00)
3. sol (1:05)
Songs: 3
Duration: 8:10
Found: Yes
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise