0

I'm making an interpreter, and I have a function called parse(). The parse function returns some output.

I'm making a command that prints two command's outputs on the same line instead of on separate lines.

I have the following code:

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

I typed in the following command to test it:

! p Hello_p World

(p is the equivalent of Python's print command.) But it outputs the following:

Hello

World

I want it to print this:

HelloWorld

What's wrong?


EDIT: The answer from this question prints the following:

Hello
World

So it doesn't work as wanted.

EDIT: Here's the parse function. Please don't mind the terrible code.

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: I'm calling two functions. I want their output to print on the same line.

Community
  • 1
  • 1
m654
  • 158
  • 1
  • 2
  • 16
  • @vaultah It's still not working; it prints `Hello (newline) World` – m654 Oct 09 '15 at 17:10
  • From where are you taking the input? File or via `input`? – Bhargav Rao Oct 09 '15 at 17:13
  • Can you verify that the string returned by the `parse()` function is not newline-terminated? – Joel C Oct 09 '15 at 17:14
  • @BhargavRao Via input. I have a `prompt` function. – m654 Oct 09 '15 at 17:14
  • 1
    I have re-opened this (as I feel I had wrongly hammered it). There is a high likelihood of the string having `\n`. Try using `command.strip()` before the `split` – Bhargav Rao Oct 09 '15 at 17:15
  • @JoelC I have this code in the `parse` function: `elif command.startswith("p "): print_command(command)`. And here's the code for the `print_command` function: `print(command[2:])` – m654 Oct 09 '15 at 17:16
  • 1
    Again, this shows the lack of a [MCVE]. We need further code to try and debug this problem. Do provide additional code if possible. – Bhargav Rao Oct 09 '15 at 17:18
  • If the `print` statement is inside your `print_command` function, that is where you will need to make the change to the `print` statement as in the marked duplicate answer. – Joel C Oct 09 '15 at 17:23
  • @BhargavRao What code do you need? Also, using `strip` before `split` did nothing. – m654 Oct 09 '15 at 17:24
  • Can you provide a reproducible sample of the `parse` function? Could you try `print(parse(command[0]).strip())`? – Bhargav Rao Oct 09 '15 at 17:27
  • @JoelC That didn't work either, unfortunately. – m654 Oct 09 '15 at 17:28
  • In that case, you will need to post a full section of code which can be run to show the issue you are having. Otherwise, we'll all just be working blind. – Joel C Oct 09 '15 at 17:31
  • @JoelC I added the whole `parse` function. – m654 Oct 09 '15 at 17:33
  • The problem seems to be from `print_command`. Have a look at those once and try to use the tips in robert's answer. – Bhargav Rao Oct 09 '15 at 17:33
  • @BhargavRao Aha, I found the problem. In the `print_command` function, I added `end=''` to the `print` command. – m654 Oct 09 '15 at 17:36
  • @BhargavRao Unfortunately, that messes up my interpreter, but I think I know how to fix _that_. – m654 Oct 09 '15 at 17:37
  • You made re-open the question though @vaultah and I had closed it correctly. Nevermind, do accept robert's answer if you feel so. – Bhargav Rao Oct 09 '15 at 17:37
  • Have you considered using something like pyparsing to help you write your parser? – shuttle87 Oct 09 '15 at 17:42
  • @shuttle87 No, but I prefer writing my parsers by hand :) (Might check pyparsing out though) – m654 Oct 09 '15 at 17:44
  • If this is a learning exercise by all means write it out by hand, however you will benefit greatly from adding some additional structure to the code as things get more complex. – shuttle87 Oct 09 '15 at 17:45

1 Answers1

0

the print() function has extra arguments you can pass to it. You are interested in end.

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


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


test1()
test2()


>>> 
hello there
>>> 

It doesn't matter how many functions this comes from

BlivetWidget
  • 10,543
  • 1
  • 14
  • 23
  • wow, a ton of buried comments on the original question, and I see that 'end' was already brought up and rejected. The question doesn't elaborate on why that doesn't have the desired effect though. – BlivetWidget Oct 09 '15 at 18:50