Kodewick
開始學 →
入門←→0 / 80

歌單第幾首

#049入門except IndexError約 3 分鐘

音箱上按一個數字就放歌單的第幾首,從 1 開始數。寫 song_at(playlist, n),返回第 n 首的歌名;歌單沒有那麼多首就返回 None。n 至少是 1。

示例
song_at(["Intro", "Rain", "Home"], 2)→'Rain'
song_at(["Intro"], 3)→None
main.py
def song_at(playlist, n):
    try:
        # 在這裡寫:返回第 n 首(列表從 0 開始數)
        pass
    except IndexError:
        return None
Ctrl + Enter 也能執行
結果

寫好了先點「執行」試試示例,沒問題再點「提交」。