简体   繁体   中英

IDLE - Python: Go back to a certain line, how can I do this?

I've created a selection menu that asks you to input the command you want to run, I've done this by using if and elif statements however when the command (the if statements) has finished I would like them to go to the line asking which command to run. Here is the code I have at the moment (Right now I have nothing that does this):

# Asks which command you want to run.
word = input("Which command? ")

# Runs the command/s.

if word == "info":
    print("Info command.")

elif word == "replace":
    print("Replace command.")

elif word == "ping":
    print("Ping command")

else:
    print("Command not found")

It would be awesome if someone could help, thanks.

Sorry if this is too much, but you might want to consider putting it into a function to do something like this:

def main():
    # Asks which command you want to run.
    word = input("Which command? ").strip().lower()

    # Runs the command/s.

    if word == "info":
        print("Info command.")

    elif word == "replace":
        print("Replace command.")

    elif word == "ping":
        print("Ping command")

    elif word == "quit":
        return False

    else:
        print("Command not found")
    return True

while main():
    pass

This would be the same thing as while True then if something: break

try wrapping your code in a while True: block. This will repeat the code indefinitely.

For further reading try this

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