2017-10-06 81 views
0

所以我试图做一个程序,用户可以输入命令到达他们要去的地方(启动另一个程序等),但是当他们输入一个命令时,他们可以到达该部分的末尾,程序停止运行可以循环回特定行吗?

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

所以我的问题是我怎么可以让它回到顶部(命令)

+1

使用'while'循环,直到他们选择退出 –

+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):") 
0

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

+0

好,谢谢这似乎有点明显的现在,我想关于它 –