Making a hollow box in Python to these specifications?
I'm to "Write a python program that prompts the user to input a positive integer n. The program then prints a hollow rectangle with n rows and 2*n columns. For a example an input of 3 would output:"
****** * * ******
And my code is:
n=int(input('Please enter a positive integer between 1 and 15: ')) for col in range(n): for row in range(n): print('*' if col in(0,(2*n)+1) or row in(0,n+1) else ' ', end=' ') print()
But my output doesn't look at all like what I need; it's like the upper and left half of a hollow box. In addition, I'm not getting double columns like I need. What am I doing wrong?
EDIT: Thank you everyone for your help! Gave me a lot of insight and was very helpful. I modified my code to the following, and it works very well:
>n=int(input('Please enter a positive integer between 1 and 15: ')) >for row in range(n): > for col in range(2*n): > print('*' if row in(0,n-1) or col in(0,(2*n)-1) else ' ', end=' ') > print()
Special shout out to user2357112; you made me realize exactly what I had tripped up.
# Response to StackOverflow post: # Making a hollow box in Python # The multiplication operator (*) is the same as repeated # concatenation when applied to strings in Python. # I solved the problem by creating an array with N elements # and gluing them together (str.join(array)) with newline # characters. # I trust you're already familiar with string formatting and # concatenation, but in case you're not, please feel free to # ask for clarification. def main(): n = int (input("Enter an integer between 1 and 15")) box = "\n".join(["*"*(2*n)] + ["*%s*" % (" "*(2*n-2))]*(n-2) + ["*"*(int(n>1)*2*n)]) print (box) if __name__ == '__main__': main() input() # Prevents the console from closing immediately
As for your solution. it seems to me like the loop conditions are messed up; the column and row loops are in reverse order and the argument to range() in the column loop should be 2*n (since that's the number of columns intersecting each row). You should also take a second look at the conditions in the first print statement.
Python Program to Print Hollow Box Pattern of , I'm to "Write a python program that prompts the user to input a positive integer n. The program then prints a hollow rectangle with n rows and 2*n columns. For my homework I need to create a square and hollow box which the dimensions of which the user chooses by entering an integer. The program would then place these boxes beside one another. Currently it prints the hollow box below the solid box.
The code should be like:
line = "*"*(2*n) print line s = "*" + " "*(n-2) + "*" for i in range(n-2): print s print line
specifies a string of "*", repeated 2*n times. You wanted 2*n column right.. This prints 2*n "*"
Program to print hollow rectangle or square star patterns , How do you print a hollow square pattern in Python? I'm trying to figure out how to turn my whole square into a hollow one. The few things I've tried so far haven't been very successful as I end up getting presented with a rather distorted triangle! The few things I've tried so far haven't been very successful as I end up getting presented with a rather distorted triangle!
def make_box(): size = int(input('Please enter a positive integer between 1 and 15: ')) for i in range(size): if i == 0 or i == size - 1: print("*"*(size+2)) else: print("*" + " "*size + "*") make_box()
***************** * * * * * * * * * * * * * * * * * * * * * * * * * * *****************
Creating a Crossed Square, Python Program to Print Hollow Box Pattern of Numbers 1 and 0 using For Loop. This Python program allows user to enter the total number of rows and columns. Program to print solid and hollow square patterns. For any given number n, print Hollow and solid Squares and Rhombus made with stars(*). Examples: 1. Solid Square : Solid Square is easiest among all given patterns. To print Solid square with n rows, we should use two loops iterating n times both.
def printStars(length): l = ['*'*length] l+= ['*' + ' '*(length-2) + '*'] * (length-2) l+= ['*'*length] return l if __name__=='__main__': print('\n'.join(printStars(15)))
*************** * * * * * * * * * * * * * * * * * * * * * * * * * * ***************
Hope this helps :)
EDIT:Fixed Some Formatting
Solved: PYTHON QUESTION Using NO Loops Or Any Other Module , Hollow rectangle star pattern : Input number of rows and columns. For rows of rectangle run the outer loop from 1 to rows. for (i = 1; i < = rows; i++) For column of rectangle run the inner loop from 1 to columns. for (j = 1; j < = columns; j++) After printing all columns of a row, print new line after inner loop. Hollow rectangle star pattern : The task is print below hollow pattern of given dimension. Explanation: Input number of rows and columns. For column of rectangle run the inner loop from 1 to columns. for (j = 1; j < = columns; j++) Print star for first or last row or for first or last column, otherwise print blank space.
My solution is more elementary. Please consider it :
row = abs(eval(input('Enter row: '))) col = abs(eval(input('Enter column: '))) print ('*'*col) for i in range (row-2): print ('*'+' '*(col-2)+'*') print ('*'*col)
1.5 Input and Output, Specs. Functions or full programs are allowed; You can get input by your preferred Yes, we use the small, classic square-shaped pixels like the old times by using the cells Python, 114 110 96 90 bytes 1 byte saved thanks to @OliverGrégoire in my answer for the Draw a hollow square of # with given width challenge; PyPA specifications¶. This is a list of currently active interoperability specifications maintained by the Python Packaging Authority. The process for updating these standards, and for proposing new ones, is documented on pypa.io. Simple repository API.
23.1. turtle — Turtle graphics, Using NO loops or any other modules (besides math), ONLY print statements, how do I get this sample output of a hollow box with dimensions in the center? Enter The Python’s barrel is marked simply with the snake styled C in “Colt” and the location of manufacture on one side and “Python .357 Magnum” on the other. The 6″ model has the traditional three cut-outs in the rib, and the 4.25″ has the two sections more traditional to the 4″ older models.
Python Tutorial, Introduction to Programming in Python by Robert Sedgewick, Kevin Wayne, and Robert The conversion specification is replaced by the argument value invoke a Java program from the command line, you actually are doing three on standard input specify the bounding box, so that it can scale the plot. Thanks for contributing an answer to Code Review Stack Exchange! Please be sure to answer the question. Provide details and share your research! But avoid … Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience. Use MathJax to format equations.
A Practical Introduction to Python Programming, After an import turtle, give it the command turtle.forward(15), and it moves place. forward/back makes turtle jump and likewise left/right make the turtle current pencolor and the current fillcolor as a pair of color specification strings or are the following polygon shapes: “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”. Making Star In Python Using Python Graphics - Duration: 2:48. AW Programming and Lectures 1,743 views. 2:48. Coding Challenge #55: Mathematical Rose Patterns - Duration: 12:44.