繁体   English   中英

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

[英]in Python can i loop back to a specific line?

因此,我正在尝试制作一个程序,用户可以在其中输入命令以到达他们要去的地方(启动另一个程序,等等。),但是当他们输入命令时,可以到达该命令的结尾(本节)和该程序停止运行

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

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

您必须循环播放,直到用户决定退出:

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

看来您需要在while循环中包围程序。 然后,让他们有一个选择,可以在完成后退出循环,结束程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM