Itertools问题-下一个函数(初级Python用户)
为了更好地理解pycharm中的while和for循环,我在这里做了一个操作音乐播放器的模拟游戏,其中包括一些我一直在学习和研究的函数和循环。你可以输入一个命令,比如“shuffle”,然后从歌曲列表中随机播放一首歌曲 问题是“下一步”命令不起作用。我无法播放列表中的“下一首”歌曲。基本上,我希望命令在每次输入歌曲时循环浏览歌曲列表。但无论我如何使用“next”函数,它仍然只返回列表中的第一首歌曲 我尝试将next()函数移到代码的其他部分,但仍然不起作用。您建议我在代码中修改什么以使“next”命令生效Itertools问题-下一个函数(初级Python用户),python,loops,iterator,next,Python,Loops,Iterator,Next,为了更好地理解pycharm中的while和for循环,我在这里做了一个操作音乐播放器的模拟游戏,其中包括一些我一直在学习和研究的函数和循环。你可以输入一个命令,比如“shuffle”,然后从歌曲列表中随机播放一首歌曲 问题是“下一步”命令不起作用。我无法播放列表中的“下一首”歌曲。基本上,我希望命令在每次输入歌曲时循环浏览歌曲列表。但无论我如何使用“next”函数,它仍然只返回列表中的第一首歌曲 我尝试将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.")
使用索引
current_song\u index
跟踪当前歌曲,在循环之前使用0
初始化它,并每执行一个“next”命令将其前进一步
随机导入
进口itertools
command=“”
player_on=False
暂停=错误
歌曲=[
“宝贝,再来一次”,
“举起手来”,
“我相信一种叫做爱的东西”,
“无羁绊的旋律”,
“来吧,艾琳”,
“我想要那样”
]
当前歌曲索引=0
尽管如此:
command=input(“您想做什么?”:”).lower()
如果命令==“播放”:
如果播放器打开且未暂停:
打印(“播放器已打开。”)
暂停=错误
elif player_打开并暂停:
暂停=错误
打印(“未暂停”)
其他:
player_on=True
暂停=错误
打印(“播放”)
elif命令==“暂停”:
如果暂停并打开播放器,请执行以下操作:
暂停=真
打印(“播放器已暂停。”)
elif player_打开且未暂停:
打印(“…”)
暂停=真
其他:
打印(“先打开播放器。”)
elif命令==“洗牌”:
如果播放器打开:
打印(“洗牌…”)
打印(随机选择(歌曲))
其他:
打印(“先打开播放器”)
elif命令==“下一步”:
如果播放器打开:
暂停=错误
当前歌曲索引+=1
如果当前歌曲索引
您将下一首歌
设置为常量值。next
的输出是iterable中的下一个值,因此它每次只查询列表一次。在您提供的代码中,在循环之前只调用了next()
一次。稍后获取next\u song
的值不会自动再次运行next()
。你每次都必须运行它。我认为len()有问题。我试图弄明白这一点,但当我用当前的歌曲索引键入正确的答案时,我得到了:Traceback(最近一次调用):File“/Users/isabellakopij/PycharmProjects/HelloWorld/MessingAround.py”,第49行,在if current歌曲索引import random
import itertools
command = ""
player_on = False
paused = False
songs = [
"Baby One More Time",
"Hands Up",
"I Believe in a Thing Called Love",
"Unchained Melody",
"Come On Eileen",
"I Want It That Way"
]
current_song_index = 0
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
current_song_index += 1
if current_song_index < len(songs):
print(f"Next song: {songs[current_song_index]}")
else:
print('End of playlist')
current_song_index = 0
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.")