0

I understand the error but I'm trying to add an if statement to it.

def repeat():
    message = input("command: ").split(" ")
    if len(message) == 2: #if there is a first parameter 
        cmd,param = message #add it to the message
    elif len(message) == 3: #if there is a first and second parameter
        cmd,param,param2 = message #add it to the message
    else: #if there is no first and second parameter
        cmd = message #add just the command to the message
    if cmd == "local":
        if len(message) == 2 or len(message) == 3: #if there is a first or second parameter
            print("error: no first or second parameter in command 'local'") #error
            repeat()
        else: #if there are no parameters
            print("test") #execute the command
            repeat()
    else:
        print("unrecognized command")
        repeat()
repeat()

Edit: When I add a second parameter to the command 'local' it returns the error on line 11, but when I do not add a first or second parameter it prints "unrecognized command" used on line 17.

johnboy13
  • 73
  • 7
  • Hint: check `print(message)` – heemayl Feb 24 '18 at 16:31
  • 1
    It's not clear which part of this code you're asking about and the example is not self-contained (by using input() there's no clear way anyone else can reproduce the error you're encountering). Make a self-contained example and clarify which part of the code you're asking about (or simplify it so this is obvious). – Jean-Paul Calderone Feb 24 '18 at 16:33
  • 1
    You are unpacking the list first and only after that doing the `if` test. The `if` test doesn't make the unpack work any differently. Put the `if` first and the unpack after that: `if len(message) == 3:`. – BoarGules Feb 24 '18 at 16:34
  • 2
    You should read the error message. It will be clear that the problem happens before you even get to the if statement – Daniel Roseman Feb 24 '18 at 16:35
  • The number of items in the message list depends on what the user enters. You should make sure your code handles invalid user input. `message = input("command: ").split(" ")` – Håken Lid Feb 24 '18 at 16:40
  • See this question for an explanation of how to use `input()` in a reliable way. https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response?noredirect=1&lq=1 – Håken Lid Feb 24 '18 at 16:42
  • I've edited my code. – johnboy13 Feb 24 '18 at 16:52

1 Answers1

0

Your code is written not so nice and you should formulate your question better, but I have fixed the error anyway.

def repeat():
    message = input("command: ").split(" ")

    #correction starts here
    lenght = len(message)
    param = False
    param2 = False
    if lenght == 1:
        cmd = message[0]
    elif lenght == 2:
        cmd = message[0]
        param = message[1]
    elif lenght == 3:
        cmd = message[0]
        param = message[1]
        param2 = message[2]
    else:
        repeat()
   #correction ends here


    if cmd == "local":
        if param: #if there is at least one parameter
            print("error: no first or second parameter in command 'local'") #error
        else: #if there are no parameters
            print(message) #execute the command
        repeat()
    if cmd == "print":
        if param2: #if there is a second parameter
            print("error: no second parameter in command 'print'") #error
        elif param: #if there is only one parameter
            print(message) #print only the first parameter (example: print test would output 'test')
        repeat()
repeat()
1cedsoda
  • 623
  • 4
  • 16