不要让下一首歌曲从播放列表中移除上一首歌曲
创始人
2025-01-10 21:00:34
0

下面是一个示例代码,演示如何实现在播放列表中不移除上一首歌曲:

class Playlist:
    def __init__(self):
        self.songs = []
        self.current_song = None

    def add_song(self, song):
        self.songs.append(song)

    def play(self):
        if self.current_song is None:
            if len(self.songs) > 0:
                self.current_song = self.songs[0]
                print("Now playing:", self.current_song)
            else:
                print("No songs in the playlist.")
        else:
            print("Resuming playback of:", self.current_song)

    def next_song(self):
        if self.current_song is None:
            print("No song is currently playing.")
        else:
            index = self.songs.index(self.current_song)
            if index < len(self.songs) - 1:
                self.current_song = self.songs[index + 1]
                print("Playing next song:", self.current_song)
            else:
                print("End of playlist.")

    def previous_song(self):
        if self.current_song is None:
            print("No song is currently playing.")
        else:
            index = self.songs.index(self.current_song)
            if index > 0:
                self.current_song = self.songs[index - 1]
                print("Playing previous song:", self.current_song)
            else:
                print("Beginning of playlist.")

# 创建一个播放列表对象
my_playlist = Playlist()

# 添加歌曲到播放列表
my_playlist.add_song("Song 1")
my_playlist.add_song("Song 2")
my_playlist.add_song("Song 3")

# 播放歌曲
my_playlist.play()

# 播放下一首歌曲
my_playlist.next_song()

# 播放上一首歌曲
my_playlist.previous_song()

这个示例中,Playlist 类有一个 add_song 方法用于添加歌曲到播放列表,play 方法用于播放歌曲,next_song 方法用于播放下一首歌曲,previous_song 方法用于播放上一首歌曲。播放列表中的歌曲存储在一个列表中,current_song 属性保存当前正在播放的歌曲。当调用 play 方法时,如果当前没有正在播放的歌曲,会播放第一首歌曲,否则会继续播放当前的歌曲。当调用 next_song 方法时,会播放下一首歌曲,如果已经是最后一首歌曲,则会提示播放列表已经结束。当调用 previous_song 方法时,会播放上一首歌曲,如果已经是第一首歌曲,则会提示已经到达播放列表的开始。

相关内容

热门资讯

透视揭露!hhpoker真能买... 透视揭露!hhpoker真能买到挂吗,hhpoker有没有辅助-一贯是有辅助app(哔哩哔哩)1、辅...
透视推荐!wpk是真的还是假的... 透视推荐!wpk是真的还是假的,wpk俱乐部有没有辅助-确实真的有辅助工具(哔哩哔哩)1、超多福利:...
透视解谜!pokermaste... 透视解谜!pokermaster破解版,云扑克有透视吗-竟然真的是有辅助工具(哔哩哔哩)1、下载好云...
透视解密!hhpoker辅助器... 透视解密!hhpoker辅助器,hhpoker的辅助是真的吗-竟然是有辅助技巧(哔哩哔哩)1、透视解...
透视了解!wpk德州局怎么透视... 透视了解!wpk德州局怎么透视,wpk真吗-果然存在有辅助攻略(哔哩哔哩)1、德州局怎么透视辅助软件...
透视揭幕!aapoker怎么控... 透视揭幕!aapoker怎么控制牌,aapoker透视脚本入口-果然有辅助app(哔哩哔哩)1、进入...
透视了解!微扑克微乐辅助,wp... 透视了解!微扑克微乐辅助,wpk俱乐部有没有辅助-一直是有辅助神器(哔哩哔哩)辅助器是一种具有地方特...
透视分享!wpk私人辅助,wp... 透视分享!wpk私人辅助,wpk私人局有透视吗-确实是有辅助脚本(哔哩哔哩)1)有没有挂:进一步探索...
透视曝光!wpk插件辅助,wp... 透视曝光!wpk插件辅助,wpk透视辅助靠谱吗-原来存在有辅助软件(哔哩哔哩)1、每一步都需要思考,...
透视科普!wpk俱乐部辅助器,... 透视科普!wpk俱乐部辅助器,如何下载wpk透视版-其实存在有辅助技巧(哔哩哔哩)该软件可以轻松地帮...