pythonif-statementpython-idle

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.


Solution

  • 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