在Python中,我可以循环回到特定的行吗?

时间:2017-10-06 16:13:47

标签: python

所以我试图创建一个程序,用户可以输入命令来到达他们要去的地方(启动另一个程序等等)但是当他们输入命令时他们可以到达那个结尾(部分)并且程序停止运行

command = input('Please enter a command or enter help for a list of commands:')
if command in ['help', 'Help', 'HELP', '?']:
    print("\t music \t Listen to music (XXXX songs)")

print("\t")
print("")

if command in ['music', 'Music']:
    print("Genres:")
print("Rap")
print("Rock")
print("Pop")
print ("Country")
print("\t\t")

genre = input('What genre do you want to listen to?:')

if genre in ['Rap', 'rap', 'RAP']:
    print("Songs (alphabetical order):")

if genre in ['Rock', 'rock', 'ROCK']:
    print("Songs (alphabetical order):")

if genre in ['Pop', 'pop', 'POP']:
    print("Songs (alphabetical order):")

所以我的问题是如何让它回到顶部(命令)

2 个答案:

答案 0 :(得分:1)

你必须循环直到用户决定退出:

command = ""

while command.lower() != 'q':
    command = input('Please enter a command or enter help for a list of commands (enter q to quit) :')

    if command in ['help', 'Help', 'HELP', '?']:
        print("     music       Listen to music (XXXX songs)")
        print("     ")
        print("")
        continue


    if command in ['music', 'Music']:
        print("Genres:")
        print("Rap")
        print("Rock")
        print("Pop")
        print ("Country")
        print("                         ")
        genre = input('What genre do you want to listen to?:')
    if genre in ['Rap', 'rap','RAP']:
        print("Songs (alphabetical order):")

    if genre in ['Rock', 'rock', 'ROCK']:
        print("Songs (alphabetical order):")

    if genre in ['Pop', 'pop','POP']:
        print("Songs (alphabetical order):")

答案 1 :(得分:0)

看起来你需要在while循环中包围你的程序。然后有一个选项让他们在完成后退出结束程序的循环。