Printing patterns - Tom Kleen
Nested LoopsPrinting patternsCreate a new Python program and name it Rectangle.Pattern #1: Creating a RectangleGet two values from the user: the height and width of the rectangle. The program should print a rectangle of stars that is the given height and width.For example, if the input is 4 for the height and 6 for the width, your program should print this:* * * * * ** * * * * ** * * * * ** * * * * *For example, if the input is 4 for the height and 4 for the width, your program should print this:* * * ** * * ** * * ** * * *Note that there is a space after each star just to make the spacing look better. If we didn't print the space, the "square" would look like this:****************Which isn't really very square. So my print commands look like this:Method 1:print("* ")But the print command puts a new line character at the end of the output. We don't want this. We can change the last character that gets printed with a print statement (the "end" character) like this:print("* ", end="")Instead of printing a new line character at the end of the line, it will print an empty string (nothing). This will keep the cursor on the same line, though.SolutionProblem-solving is looking for patterns. It may be easier to see patterns if we write a few things down. Create a table that describes each row and lists all of the related variables:RowHeightWidthNumber of stars0466146624663466It is obvious that the number of stars is always the same on each row – the same number of stars as Width. Below is the easiest way to solve the problem.height = int(input("Enter height: "))width = int(input("Enter width:"))for r in range(height): print("* " * width)However, this lesson is about nested loops (loops inside of loops), so I want you to replace the print statement with another for loop that will print one star each time, like this:print("* ", end="") So the cursor will stay on the same line after printing the asterisk. New code:for r in range(height): for c in range(width): print("* ", end="") print()Note that the final print moves the cursor to a new line.Pattern #2: Creating a right triangle (left side)Write code that inputs a positive integer representing the number of asterisks in the base of a triangle and draws the triangle with one star on the first row, two stars on the second row, three on the third row, etc.For example, if the input was 4, the output would be:* * * * * * * * * * SolutionAgain, create a table and look for patterns:rowheightstars041142243344It is obvious that the row number determines how many stars to print.Pattern #3: Creating an upside-down triangleDo the same thing as the last problem, but invert the triangle, with the longest line of stars at the top.For example, if the number input by the user is 4:**********SolutionCreate a table and look for patterns:rowrowsstars044143242341Pattern #4: Creating a right triangle (right-side)Do the same as the first problem, but with the right angle at the bottom right side of the triangle rather than at the bottom left.For example, if the number input by the user is 4: * ** *** **** SolutionCreate a table and look for patterns:rowrowsblanksstars0431142224133404Note that there will be two inner loops: one for blanks and one for stars.Pattern #5: Creating a pyramidThe user will enter the number of rows. ExamplesNumber of rows is 2: * ***Number of rows is 3: * *** ***** Number of rows is 4: * *** ***** *******Make a table. Write the row number in the first column. In the second column, write the number of stars. In the third column, write the number of leading spaces. Then derive formulas for the number of stars and the number of leading spaces. Then write your algorithm.SolutionCreate a table and look for patterns:rowrowsblanksstars0431142324153407 ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- printing patterns tom kleen
- python class room diary be easy in my python class
- python for kids lesson plan no starch press
- lecture 1 depaul university
- python part iii repeating actions with loops
- ucf computer science
- laboratory manual for computer programming with python
- read the whole file as a string tom kleen
- file i o benford s law sets
Related searches
- free watercolor patterns to trace
- seeing patterns in vision
- free watercolor patterns to print
- watercolor painting patterns free printable
- world history patterns of interaction
- gordon s functional health patterns questions
- geometric patterns in vision
- sentence patterns in english grammar
- seeing patterns in eyes
- gordon s functional health patterns pdf
- functional health patterns assessment tool
- free wood craft patterns catalogs