DRauch
DRauch

Reputation: 57

Multiple Variables Will not Print to One Line

I have searched through plenty of stackoverflow and other help sites for an answer to this but none of the solutions seem to have worked.

I am new to Python so this is probably a really easy fix, I just cannot get it to work using global x, nonlocal x, print(x,end='') or the other suggestions I have found.

I have tried assigning variables to (i.e. a = 'Message') if certain conditions are met within a data set. I want to then print those variables at the end of the function. I am able to get it to print on multiple lines using the print function after each statement, but I want all the strings to print to one line.

My Code:

column = [int(v) for v in entries[3:]]
def function():
    if column[0] == 100:
        print('Message')
        if column[1] >= 200 and column[2] == 300:
            if len(column) >= 9:
                digit = [chr(x) for x in column[4:9]]
                for l in range(0, 5):
                    print('Message Update: ' + digit[l])
            if len(column) >= 13:
                optional_digit = [chr(d) for d in column[9:13]]
                for m in range(0, 4):
                    print('Optional Field: ' + optional_digit[m])
            else:
                print('No Optional Field')
        else:
            print('Not a Message Update)
    else:
        print('Not a Message')
    print('')
function()

In a perfect world, and assuming data sets meet my conditions, I would like to see something like "Message Message Update 542" for one data set after my function is executed on one single line.

Please help! :)

Upvotes: 0

Views: 114

Answers (4)

Zev Averbach
Zev Averbach

Reputation: 1134

column = [int(v) for v in entries[3:]]

def function():
    print_string = ''
    if column[0] == 100:
        print_string += 'Message '
        if column[1] >= 200 and column[2] == 300:
            if len(column) >= 9:
                digit = [chr(x) for x in column[4:9]]
                for l in range(0, 5):
                    if 'Message Update: ' in print_string:
                        print_string += digit[l]
                    else:
                        print_string += f'Message Update: {digit[l]}'
            if len(column) >= 13:
                optional_digit = [chr(d) for d in column[9:13]]
                for m in range(0, 4):
                    if 'Optional Field: ' in print_string:
                        print_string += optional_digit[m]
                    else:
                        print_string += f'Optional Field: {optional_digit[m]}'
            else:
                print_string += 'No Optional Field '
        else:
            print_string += 'Not a Message Update '
    else:
        print_string += 'Not a Message '
    print(print_string) 

function()

Upvotes: 0

Igor Yudin
Igor Yudin

Reputation: 403

Try to append all parts of message to one array and then print them

column = [int(v) for v in entries[3:]]
def function():
    message = []
    if column[0] == 100:
        message.append('Message')
        if column[1] >= 200 and column[2] == 300:
            if len(column) >= 9:
                digit = [chr(x) for x in column[4:9]]
                message.append('Message Update:')
                message.append(''.join(str(digit[l]) for l in range(5)))
            if len(column) >= 13:
                optional_digit = [chr(d) for d in column[9:13]]
                for m in range(0, 4):
                    message.append('Optional Field: ' + optional_digit[m])
            else:
                message.append('No Optional Field')
        else:
            message.append('Not a Message Update)
    else:
        message.append('Not a Message')
    print(*message, end='')
function()

EDIT

It seems you want to get string like 'Message Message Update 123', so you should simply create a sequence of this numbers and append/concantenate them after

Upvotes: 0

silent_sight
silent_sight

Reputation: 502

You could construct one string where you have your print statements and then print it all at the end instead of as you go.

column = [int(v) for v in entries[3:]]
def function():
    if column[0] == 100:
        msg = 'Message'
        if column[1] >= 200 and column[2] == 300:
            if len(column) >= 9:
                digit = [chr(x) for x in column[4:9]]
                for l in range(0, 5):
                    msg = '%s Message Update: %d' %(msg, digit[l])
            if len(column) >= 13:
                optional_digit = [chr(d) for d in column[9:13]]
                for m in range(0, 4):
                    msg = '%s\n\tOptional Field: %d' %(msg, optional_digit[m])
            else:
                msg = '%s\n\tNo Optional Field' %msg
        else:
            msg = '%s Not a Message Update' %msg
    else:
        msg = '%s Not a Message' %msg
    print(msg)
function()

I wasn't sure if you wanted the optional field in the same line - I actually dropped it to a new line and tabbed over once, but you can easily remove that if you want it all on the same line.

Upvotes: 1

MrPromethee
MrPromethee

Reputation: 719

To remove the line feed at the end of a python (python3) print call it this way:

print(your_string, end="")

Upvotes: 0

Related Questions