如何解决Itertools的问题-下一个函数初级Python用户
我在pycharm中使用while和for循环弄乱,以便更好地理解它们,我创建了一个操作音乐播放器的模型游戏,其中涉及一些我一直在研究和研究的功能和循环。例如您输入了“随机播放”之类的命令,就会播放歌曲列表中的随机歌曲。
问题是“下一个”命令不起作用。我无法从列表中播放“下一首”歌曲。本质上,我希望该命令在每次输入时都能在歌曲列表中循环显示。但是无论我何时使用“ next”功能,它仍然只返回列表中的第一首歌曲。
我尝试改为将next()函数移至代码的其他部分,但仍然无法正常工作。您建议我在代码中做些什么更改才能使“下一个”命令正常工作?
import random
import itertools
command = ""
player_on = False
paused = False
songs = iter([
"Baby One More Time","Hands Up","I Believe in a Thing Called Love","Unchained Melody","Come On Eileen","I Want It That Way"
])
next_song = next(songs,"end of playlist")
while True:
command = input("What do you want to do?: ").lower()
if command == "play":
if player_on and not paused:
print("Player is already on.")
paused = False
elif player_on and paused:
paused = False
print("un-paused")
else:
player_on = True
paused = False
print("Playing.")
elif command == "pause":
if paused and player_on:
paused = True
print("player already paused.")
elif player_on and not paused:
print(". . .")
paused = True
else:
print("Turn player on first.")
elif command == "shuffle":
if player_on:
print("Shuffles . . .")
print(random.choice(songs))
else:
print("Turn player on first")
elif command == "next":
if player_on:
paused = False
print(f"Next song: {next_song}")
else:
print("Turn player on first.")
elif command == "quit":
if not player_on:
print("Player is already off.")
else:
player_on = False
break
else:
print("I don't understand that command.")
解决方法
使用索引df_curr = df_curr[sorted(df_curr)]
来跟踪当前歌曲,在循环之前使用current_song_index
对其进行初始化,并在每条“ next”命令中将其前进。
0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。