Harry
Harry

Reputation: 13

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.

Upvotes: 0

Views: 22914

Answers (2)

jackcogdill
jackcogdill

Reputation: 5122

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

Upvotes: 1

tympaniplayer
tympaniplayer

Reputation: 171

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

For further reading try this

Upvotes: 0

Related Questions