Itertools 的问题 - 下一个函数(初级 Python 用户)

Problem with Itertools - next function (beginner-level Python user)

我正在研究 pycharm 中的 while 和 for 循环以更好地理解它们,并且我创建了一个操作音乐播放器的模型游戏,其中涉及我一直在学习和研究的一些函数和循环。前任。您输入一个命令,例如“随机播放”,就会播放歌曲列表中的随机歌曲。

问题是“下一个”命令不起作用。我无法从列表中播放“下一首”歌曲。本质上,我希望命令在每次输入时循环播放歌曲列表。但是无论我如何使用“下一首”功能,它仍然只是returns列表中的第一首歌曲。

我尝试将 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_index 来跟踪当前歌曲,在循环之前用 0 初始化它,并在每个“下一个”命令时前进。

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.")