我理解错误,但我尝试向其添加if
语句。
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()
编辑:当我向命令添加第二个参数时,本地'它在第11行返回错误,但是当我没有添加第一个或第二个参数时,它会打印"无法识别的命令"在第17行使用。
答案 0 :(得分:0)
您的代码写得不太好,您应该更好地制定您的问题,但我已经修复了错误。
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()