简体   繁体   中英

in Python can i loop back to a specific line?

So I am trying to make a program where the user can enter commands to get to where they are going (start another program, etc..) but when they enter a command they can get to the end of that (section) and the program stops running

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

So my question is how can I make it go back to the top (command)

You have to loop until user decides to quit:

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

It looks like you would need to surround your program in a while loop. Then have an option for them to exit the loop ending the program when they are finished.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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