开发者社区 问答 正文

在Python中超出范围后,如何重新启动列表?

我正在尝试创建一种快速工具,用于以音乐音阶查看音符。用户输入音阶名称,程序会吐出音符。例如,您输入C,然后输出显示“ C,D,E,F,G,A,B”。我只设置了前两个音符,没有平音或锐音。为了使所有内容保持整洁,我想从单个数组构建缩放比例,只是我不知道如何循环遍历该数组。如果用户输入G,则会显示一条错误消息,提示您超出了范围。我该如何做,以便用户输入G后打印G“ G,B?”

notes = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

scale_name = input('enter scale: ')

def scale_checker():
    if scale_name in notes:
        return(scale_name)
    elif scale_name not in notes:
        print('please enter valid scale')

start = notes.index(scale_name)

next_note = start +1 

print(notes[start],notes[next_note])

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 00:12:43 444 分享 版权
1 条回答
写回答
取消 提交回答
  • 您可以使用切片将值从“开始”打印到列表的末尾,从列表的开头打印到“开始”。

    start = notes.index(scale_name)
    print(\*otes[start:] + notes[:start])
    

    回答来源:stackoverflow

    2020-03-24 00:12:49
    赞同 展开评论
问答分类:
问答标签:
问答地址: