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...

  • 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!")...

  • //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...

  • 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...

  • 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 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:...

  • 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...

  • Throughout this script, can you provide helpful comments about how each function works in the script...

    Throughout this script, can you provide helpful comments about how each function works in the script plus it's significance to the alignment process. Also are there any errors in this script or a way to improve this script? Any help would be appreciated. Thank you. THIS IS A PYTHON CODE AND IS IN IT'S FORMAT _ ITS CLEAR! #!/usr/bin/env python # file=input("Please provide fasta file with 2 sequences:") match=float(input('What is the match score?:')) missmatch=float(input('What is the missmatch score?:')) gap=float(input('What is...

  • Using java : In this exercise, you need to implement a class that encapsulate a Grid. A grid is a useful concept in crea...

    Using java : In this exercise, you need to implement a class that encapsulate a Grid. A grid is a useful concept in creating board-game applications. Later we will use this class to create a board game. A grid is a two-dimensional matrix (see example below) with the same number of rows and columns. You can create a grid of size 8, for example, it’s an 8x8 grid. There are 64 cells in this grid. A cell in the grid...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do 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...

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
ADVERTISEMENT