[英]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.