Question

python: how would I format the grid below so that it is aligned correctly? as you...

python: how would I format the grid below so that it is aligned correctly? as you see, I tried to center it but it is still not correct.

import random
row=random.randint(1,10)
col=random.randint(1,10)
for r in range(row):
for c in range(col):
if r %2==0 and c %2==0:
print(format('a','^3'),end=' ')
elif r %2!=0 and c %2!=0:
print(format("/x\\",'^3'),end=' ')
elif c%2!=0:
print(format('-','^3'),end='')
  
else:
print(format('s','^3'),end=' ')   
print() #go to next line

current Output is: (example if random function choses col=4 and row=3)

a - a -   
s /x\ s /x\   
a - a -   

Expected output: Rows Should be aligned so that each row and column line up together

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import random
row=random.randint(1,10)
col=random.randint(1,10)
for r in range(row):
    for c in range(col):
        if r %2==0 and c %2==0:
            print('{:^3s}'.format('a'),end='')
        elif r %2!=0 and c %2!=0:
            print('{:^3s}'.format('/x\\'),end='')
        elif c%2!=0:
            print('{:^3s}'.format('-'),end='')
        else:
            print('{:^3s}'.format('s'),end='')
    print()

Add a comment
Know the answer?
Add Answer to:
python: how would I format the grid below so that it is aligned correctly? as you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How can I get my Python code to check to make sure there are no ships...

    How can I get my Python code to check to make sure there are no ships overlapping in my Battleship code? I am having a really hard time with a game of battleship that I have due for class. Each time I run my code, It will print the ships on the board but it will at times overlap them. I know I have to use some kind of nested if statements but I just don't get how or where...

  • Having a rough time getting started with the constructors and the display function of this class,...

    Having a rough time getting started with the constructors and the display function of this class, any advice/assitance is appreciated! Task You will write a class called Grid, and test it with a couple of programs. A Grid object will be made up of a grid of positions, numbered with rows and columns. Row and column numbering start at 0, at the top left corner of the grid. A grid object also has a "mover", which can move around to...

  • 2D Lists + File I/O In a comma-separated input file named results.txt, you have been given...

    2D Lists + File I/O In a comma-separated input file named results.txt, you have been given the following information that records the weekly (movie) box office sales for 5 movies. A sample input file will include the following. The movie’s title is listed first, then its sales (in million dollars) for 7 days are listed. Avengers,169.1,125.8,101.7,40.5,38.2,24.2,55.7 Shazam!,8.6,14.1,8.2,7.3,31.4,44.2,26.8 Breakthrough,14.8,16.1,18.0,18.9,19.8,21.8,24.6 The Best of Enemies,4.7,5.4,5.8,6.1,6.7,7.6,8.1 Dumbo,9.9,14.8,9.0,7.9,40.6,52.5,36.3 Write a complete Python program that includes code to do the following: read in the data from...

  • print(" |",end=' ') for i in range(1,10): print(i,end=' ') print() for i in range(0,11): print('-',end=' ')...

    print(" |",end=' ') for i in range(1,10): print(i,end=' ') print() for i in range(0,11): print('-',end=' ') print() for i in range(1,10): for j in range(0,11): if j==0: print(i,end=' ') elif j==1: print('|',end=' ') elif i==9 and j==10: print(i*(j-1),end=' ') elif i>=4 and j>=4: print('*',end=' ') else: print(i*(j-1),end=' ') print() This code is not working I need help 1 2 3 4 5 6 7 8 9 --- --- -------------------- -------------------- 5 10 15 X 6 12 18 X 7 8...

  • For this lab, you will work with two-dimensional lists in Python. Do the following: Write a...

    For this lab, you will work with two-dimensional lists in Python. Do the following: Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: def sumColumn(matrix, columnIndex) Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single space to separate different values. Sample output from this function when it...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • Can you please enter this python program code into an IPO Chart? import random def menu():...

    Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...

  • Write a module of utility functions called util.py for manipulating 2-dimensional arrays of size 4x4. (These...

    Write a module of utility functions called util.py for manipulating 2-dimensional arrays of size 4x4. (These functions will be used in Question 3.) The functions you need to write are as follows: def create_grid(grid): """create a 4x4 array of zeroes within grid""" def print_grid (grid): """print out a 4x4 grid in 5-width columns within a box""" def check_lost (grid): """return True if there are no 0 values and there are no adjacent values that are equal; otherwise False""" def check_won...

  • in java / You will: // 1- create a square 2 dimensional array of random size...

    in java / You will: // 1- create a square 2 dimensional array of random size (less than 11) // 2- fill the array with random integers from 1 to the size limit // 3- print the array // 4- prompt to see if the user wants to do another //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // 1- The square can use the same random value for x and y. Don't allow 0 size arrays. // 2- Maybe a nested set of for loops? to...

  • In python this is what I have for task 3, but I don't know how to...

    In python this is what I have for task 3, but I don't know how to fix it so that the computer stops picking from the pile once it reaches zero Task 3: Extend the game to have two piles of coins, allow the players to specify which pile they take the coins from, as well as how many coins they take Finish Task 3 and try three test cases import random remaining_coins1 = 22 remaining_coins2 = 22 while 1:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT