How to append strings from a loop to a single output line?

Samoknight

So I am trying to make a text-based game of connect-4 for the purposes of better understanding Python and how it actually works.

Short version

  • How can I append printed text from every run-through of a while loop to a print output that exists just before the while loop
  • Out of the two methods seen below (The work in progress and the current successfully working one) which is a better practice of executing the desired output?

Long version

I am trying to use a looping system to print out an array in an evenly spaced and aesthetically pleasing format after every turn is taken, so users have clear feedback of what the current board looks like before the next turn is taken. To do this I want to be able to have lines of code that are as small as possible for making it easier to read the code itself. Although this might not be the best practice for executing this scenario I want to understand this way of coding better so I could apply it to future projects if need be.

In terms of the actual execution, I am trying to use a while loop to append 7 positions of an array one after another in the same output line for array positions that are in the same row. after this, I want to print the next row on the line below the previous one as seen in the code below "Desired output".

Thank you in advance for your answers, suggestions and comments.

Work in progress

import numpy as np
ARRAY = np.zeros(shape=(6, 7), dtype = 'int8')
# In reality I will be using an empty array that gradually gets populated
# Zeros are used for ease of asking the question

def Display_board():
    i = 0
    while i < 7:
        j = 0
        print("  ", end = " ")
        while j < 8:
            print(str(ARRAY[i][j]))
            j += 1
        i += 1

work in progress output

 0
   0
0
0
0
0
0
0
   0
0
0
0
0
0
0
# It goes on but didn't include as it would take up unnessary space in the question

If I change the line that prints the array to as follows I get another undesired output

print(str(ARRAY[i][j]), end = " ")

#output
  0 0 0 0 0 0 0    0 0 0 0 0 0 0    0 0 0 0 0 0 0    0 0 0 0 0 0 0    0 0 0 0 0 0 0    0 0 0 0 0 0 0

Current working method - Gives desired output

def Display_board():
   for i in range(6):
       print("   " + str(ARRAY[i][0]) + "  " + str(ARRAY[i][1]) + "  " + str(ARRAY[i][2]) \
       + "  " + str(ARRAY[i][3]) + "  " + str(ARRAY[i][4]) + "  " + str(ARRAY[i][5])\
       + "  " + str(ARRAY[i][6]))

Desired output

  0  0  0  0  0  0  0
  0  0  0  0  0  0  0
  0  0  0  0  0  0  0
  0  0  0  0  0  0  0
  0  0  0  0  0  0  0
  0  0  0  0  0  0  0
Nick

The simple fix is to use end=' ' on the print inside the while loop on j and then add a print() after it:

def Display_board():
    i = 0
    while i < 6:
        j = 0
        print("  ", end = " ")
        while j < 7:
            print(str(ARRAY[i][j]), end=" ")
            j += 1
        print()
        i += 1

Output:

   0 0 0 0 0 0 0 
   0 0 0 0 0 0 0 
   0 0 0 0 0 0 0 
   0 0 0 0 0 0 0 
   0 0 0 0 0 0 0 
   0 0 0 0 0 0 0 

You can also use a nested list comprehension with join to achieve the output in one line:

def Display_board():
    print('\n'.join(' '.join(['  '] + [str(ARRAY[i][j]) for j in range(7)]) for i in range(6)))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to append a line with the output of a command using ed

Append to an empty list using a single line for-loop python

How to append strings to a string in a loop?

How to append multiple strings from a single input into a list in Python

How to cast the output of a for loop into a single list of strings in Python?

Extract specific strings from a single long line

How to save single line from file to two different strings¸in struct >> line format(NAME#SURNAME AGE)

How would I append an integer to a single line from my bot?

How to append strings to a vector from a for loop?

Windows command line: How to append a variable in a loop?

append text to an output from command line

Read integers and strings from a single line of a console

Loop iteration and output result in single line.

How to append output to a specific line in a file

How to compare lines in a single file line by line and append the output to the same file

How to display output on single line?

How to append line with strings at the end of variable?

Extract single line from command output in terminal

I'm trying to get the output from my for loop to print in a single line in the console

How to append a character at the end of a specific line in a loop?

How to output values from 2 different groups onto a single line?

How to append new line \n in a loop

How to append an output that is looping rather than making each loop its own line

How to print output of a for loop in a single line in Bash

How to remove last whitespace of a single line output inside a loop?

How to convert the output into a single line

How to append Psypark FOR loop output into single dataframe (spark.sql)

MySQL: Append the columns of multiple SELECT queries as a single output line

How to concatenate lists into single merged DataFrame from for loop output?