使两个函数在同一行上打印输出

时间:2015-10-09 17:06:56

标签: python python-3.x interpreter

我正在翻译,我有一个名为parse()的函数。 parse函数返回一些输出。

我正在制作一个命令,在同一行上打印两个命令的输出而不是在不同的行上。

我有以下代码:

def print_command2(command):
    command = command.split("_") # The underscore separates the two commands
    command[0] = command[0].strip("! ") # The command is !
    print(parse(command[0])), # This should print the output of the first command
    print(parse(command[1])) # And this should print the second

我输入以下命令来测试它:

! p Hello_p World

p相当于Python的print命令。) 但它输出如下:

Hello

World

我希望它打印出来:

HelloWorld

怎么了?

编辑:this question的答案打印出以下内容:

Hello
World

所以它不能按预期工作。

编辑:这是parse功能。请不要介意可怕的代码。

def parse(command):
    """Parses the commands."""
    if ';' in command:
        commands = command.split(";")
        for i in commands:
            parse(i)
    if '\n' in command:
        commands = command.split('\n')
        for i in commands:
            parse(i)
    elif command.startswith("q"):
        quine(command)
    elif command.startswith("p "):
        print_command(command)
    elif command.startswith("! "):
        print_command2(command)
    elif command.startswith("l "):
        try:
            loopAmount = re.sub("\D", "", command)
            lst = command.split(loopAmount)
            strCommand = lst[1]
            strCommand = strCommand[1:]
            loop(strCommand, loopAmount)
        except IndexError as error:
            print("Error: Can't put numbers in loop")
    elif '+' in command:
        print (add(command))
    elif '-' in command:
        print (subtract(command))
    elif '*' in command:
        print (multiply(command))
    elif '/' in command:
        print (divide(command))
    elif '%' in command:
        print (modulus(command))
    elif '=' in command:
        lst = command.split('=')
        lst[0].replace(" ", "")
        lst[1].replace(" ", "")
        stackNum = ''.join(lst[1])
        putOnStack(stackNum, lst[0])
    elif command.startswith("run "):
        command = command.replace(" ", "")
        command = command.split("run")
        run(command[1])
    elif command.startswith('#'):
        pass
    elif command.startswith('? '):
        stackNum = command[2]
        text = input("input> ")
        putOnStack(text, stackNum)
    elif command.startswith('@ '):
        stackNum = command[2]
        print(''.join(stack[int(stackNum)]))
    elif command.startswith("."):
        time.sleep(2)
    else:
        print("Invalid command")
    return("")

TL; DR:我正在调用两个函数。我希望他们的输出打印在同一行。

1 个答案:

答案 0 :(得分:0)

print()函数有额外的参数可以传递给它。您对end感兴趣。

def test1():
    print('hello ', end='')


def test2():
    print('there', end='')


test1()
test2()


>>> 
hello there
>>> 

来自

的功能并不重要