Chapter 8 of python3, python3

Source: Internet
Author: User

Chapter 8 of python3, python3

We dug a hole in Chapter 4: how to print the 9-9 multiplication table in an elegant way. We will fill in this chapter.

First, let's take a look at what the multiplication table looks like.

1 x 1 = 11 x 2 = 2 2 x 2 = 41 x 3 = 3 2 x 3 = 6 3 x 3 = 91 x 4 = 4 2 x 4 = 8 3 x 4 =12 4 x 4 =161 x 5 = 5 2 x 5 =10 3 x 5 =15 4 x 5 =20 5 x 5 =251 x 6 = 6 2 x 6 =12 3 x 6 =18 4 x 6 =24 5 x 6 =30 6 x 6 =361 x 7 = 7 2 x 7 =14 3 x 7 =21 4 x 7 =28 5 x 7 =35 6 x 7 =42 7 x 7 =491 x 8 = 8 2 x 8 =16 3 x 8 =24 4 x 8 =32 5 x 8 =40 6 x 8 =48 7 x 8 =56 8 x 8 =641 x 9 = 9 2 x 9 =18 3 x 9 =27 4 x 9 =36 5 x 9 =45 6 x 9 =54 7 x 9 =63 8 x 9 =72 9 x 9 =81

It is not difficult to find that there are the following rules:

  • A total of 9 rows, a maximum of 9 Columns
  • Number of Columns with the same number of rows per row
  • From top to bottom, they are 1st ~ 9 rows; from left to right, respectively 1st ~ 9 Columns
  • For each row, the content in the cell is the row number multiplied by the column number, which is a loop

 

Let's first output each cell in nine rows and nine columns.

For row in range (1, 10): for column in range (1, 10): if column = 9: print ('dide', row, 'row dide ', column, 'column ') else: print ('column', row, 'row ', column, 'column', end = '')

The above code outputs:

1st rows, 1st columns, 1st rows, 2nd columns, 1st columns, 3rd columns, 1st columns, 4th columns, 1st columns, 5th columns, 1st columns, 6th Columns
2nd rows, 1st columns, 2nd rows, 2nd columns, 2nd columns, 3rd columns, 2nd columns, 4th columns, 2nd columns, 5th columns, 2nd columns, 6th Columns
3rd rows, 1st columns, 3rd rows, 2nd columns, 3rd columns, 3rd columns, 3rd columns, 4th columns, 3rd columns, 5th columns, 3rd columns, 6th Columns
4th rows, 1st columns, 4th rows, 2nd columns, 4th columns, 3rd columns, 4th columns, 4th columns, 4th columns, 5th columns, 4th columns, 6th Columns
5th rows, 1st columns, 5th rows, 2nd columns, 5th columns, 3rd columns, 5th columns, 4th columns, 5th columns, 5th columns, 5th columns, 6th Columns
6th rows, 1st columns, 6th rows, 2nd columns, 6th columns, 3rd columns, 6th columns, 4th columns, 6th columns, 5th columns, 6th columns, 6th Columns
7th rows, 1st columns, 7th rows, 2nd columns, 7th columns, 3rd columns, 7th columns, 4th columns, 7th columns, 5th columns, 7th columns, 6th Columns
8th rows, 1st columns, 8th rows, 2nd columns, 8th columns, 3rd columns, 8th columns, 4th columns, 8th columns, 5th columns, 8th columns, 6th Columns
9th rows, 1st columns, 9th rows, 2nd columns, 9th columns, 3rd columns, 9th columns, 4th columns, 9th columns, 5th columns, 9th columns, 6th Columns

 

In fact, this does not achieve what we want, because the 9-9 multiplication table has only one column in the first row, and a column is added to each row. we modify the program:

For row in range (1, 10): maxColumn = row + 1 for column in range (1, maxColumn): if column = row: print ('dide', row, 'row de', column, 'column ') else: print ('column', row, 'row de', column, 'column ', end = '')

The above code outputs:

1st rows, 1st columns, 2nd rows, 1st columns, 2nd columns, 2nd columns, 3rd columns, 1st columns, 3rd columns, 2nd columns, 3rd columns, 3rd columns, 4th columns 5th rows, 1st columns, 5th rows, 2nd columns, 5th columns, 3rd columns, 5th columns, 4th columns, 5th columns, 5th columns, 6th columns, 1st columns, 6th Columns 6th rows, 6th columns, 7th rows, 1st columns, 7th columns, 2nd columns, 7th columns, 3rd columns, 7th columns, 4th columns, 7th columns, 5th columns, 7th Columns 8th rows, 3rd columns, 8th rows, 4th columns, 8th columns, 5th columns, 8th columns, 6th columns, 8th columns, 7th columns, 8th columns, 8th columns, 9th Columns 9th rows, 5th columns, 9th rows, 6th columns, 9th columns, 7th columns, 9th columns, 8th Columns

Here, we find that the form is already in use. We just need to fill in the calculation on each cell.

for row in range(1, 10):    maxColumn = row + 1    for column in range(1, maxColumn):        if column == row:            print(column, 'x', row, '=', column * row, ' ')        else:            print(column, 'x', row, '=', column * row, ' ', end='')

The above code outputs:

1 x 1 = 1  1 x 2 = 2  2 x 2 = 4  1 x 3 = 3  2 x 3 = 6  3 x 3 = 9  1 x 4 = 4  2 x 4 = 8  3 x 4 = 12  4 x 4 = 16  1 x 5 = 5  2 x 5 = 10  3 x 5 = 15  4 x 5 = 20  5 x 5 = 25  1 x 6 = 6  2 x 6 = 12  3 x 6 = 18  4 x 6 = 24  5 x 6 = 30  6 x 6 = 36  1 x 7 = 7  2 x 7 = 14  3 x 7 = 21  4 x 7 = 28  5 x 7 = 35  6 x 7 = 42  7 x 7 = 49  1 x 8 = 8  2 x 8 = 16  3 x 8 = 24  4 x 8 = 32  5 x 8 = 40  6 x 8 = 48  7 x 8 = 56  8 x 8 = 64  1 x 9 = 9  2 x 9 = 18  3 x 9 = 27  4 x 9 = 36  5 x 9 = 45  6 x 9 = 54  7 x 9 = 63  8 x 9 = 72  9 x 9 = 81  

So far, the success is achieved.

 

Thought 1: What should I do if print () appears only once in a program? The answer below is provided. do not view the answer before writing it on your own.

For row in range (1, 10): maxColumn = row + 1 for column in range (1, maxColumn): endString = ''# Terminator if column = row: endString = '\ n' # print (column, 'x', row,' = ', column * row, '', end = endString)
View Code

Thinking 2: we can see that some columns in the 9-9 multiplication table output by the above program are not aligned up and down. How can we align them? The answer below is provided. do not view the answer before writing it on your own.

For row in range (1, 10): maxColumn = row + 1 for column in range (1, maxColumn ): endString = ''# splitString ='' # delimiter between the product and equal sign. The default Delimiter is space num = column * row # product if num> 9: splitString = ''if column = row: endString = '\ n' # print (column, 'x', row,' = ', splitString, num, '', end = endString)
View Code

Think 3: How to output a multiplication table in reverse mode? The answer below is provided. do not view the answer before writing it on your own.

RowsCount = 9 # The largest row while rowsCount> 0: maxColumn = rowsCount + 1 for column in range (1, maxColumn ): endString = ''# separator between the terminator splitString ='' # And equal sign. The default Delimiter is space num = column * rowsCount # product if num> 9: splitString = ''if column = rowsCount: endString = '\ n' # print (column, 'x', rowsCount,' = ', splitString, num, '', end = endString) rowsCount-= 1
View Code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.