簡體   English   中英

使兩個函數在同一行上打印其輸出

[英]Making two functions print their output on the same line

我正在做一個解釋器,並且有一個名為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

怎么了?


編輯: 此問題的答案打印以下內容:

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:我正在調用兩個函數。 我希望它們的輸出打印在同一行上。

print()函數還有其他參數可以傳遞給它。 您對end感興趣。

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


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


test1()
test2()


>>> 
hello there
>>> 

多少功能無關緊要

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM