Hướng dẫn how to make a table in python using for loop - cách tạo bảng trong python bằng vòng lặp for

Trong khi tạo ra một bảng trên Python, tôi gặp phải một vấn đề. Đây là mã:

row = 5
col = 4
for x in range[1, row + 1]:
    print["Row", x, end=""]
    for y in range[1, col + 1]:
        print[" Column", y, end=""]
        print[" Row", x, end=""]
    print[]

Đây là những gì chạy:

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5

Như bạn có thể thấy nó kết thúc bằng các hàng và bị thiếu cột 5. Tôi có thể làm gì để khắc phục điều này?

Đây là đầu ra mà tôi mong đợi:

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1 Column 5
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2 Column 5
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3 Column 5
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4 Column 5
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5 Column 5

Trong chương trình dưới đây, chúng tôi đã sử dụng vòng lặp For để hiển thị bảng nhân là 12.

Mã nguồn

# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]

Đầu ra

12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120

Ở đây, chúng tôi đã sử dụng vòng

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
2 cùng với hàm phạm vi [] để lặp lại 10 lần. Các đối số bên trong hàm
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
3 là [1, 11]. Ý nghĩa, lớn hơn hoặc bằng 1 và nhỏ hơn 11.

Chúng tôi đã hiển thị bảng nhân của NUM biến [là 12 trong trường hợp của chúng tôi]. Bạn có thể thay đổi giá trị của NUM trong chương trình trên để kiểm tra các giá trị khác.

Goodness Chidinma Abarugo

Tổng quan

Trong Python, chúng ta có thể tạo một bảng nhân cho bất kỳ số nào bằng cách kết hợp các hàm

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
4 và
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
3 với câu lệnh LOOP.

Hàm
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
4

Hàm

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
4 được sử dụng để chấp nhận đầu vào từ người dùng. Nếu kiểu dữ liệu cần thiết không được xác định rõ ràng, bất kỳ giá trị nào được cung cấp bởi người dùng tại dấu nhắc được lưu trữ trong bộ nhớ dưới dạng chuỗi.
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
4 function
is used to accept input from the user. If the required data type is not explicitly defined, any value provided by the user at the prompt is stored in memory as a string.

Cú pháp

input[prompt]

Tham số

  • Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
    Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
    Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
    Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
    Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
    
    8: Một chuỗi được bao quanh trong các trình điều khiển đơn hoặc kép. Sự hiện diện của nó làm cho mã của chúng tôi tương tác nhiều hơn. Nó là một tham số tùy chọn.
    : A string enclosed in single or double-quotes. Its presence makes our code more interactive. It is an optional parameter.

Hàm
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
3

Hàm

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
3 cho phép chúng tôi tự động tạo một loạt các số. Chúng tôi lặp lại loạt trong khi thực hiện một số dòng mã cho đến khi chúng tôi đến số cuối cùng trong chuỗi.
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
3 function
allows us to create a series of numbers automatically. We iterate over the series while executing some lines of code until we arrive at the last number in the series.

Cú pháp

range[start, stop, step]

Tham số

  • Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
    Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
    Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
    Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
    Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
    
    8: Một chuỗi được bao quanh trong các trình điều khiển đơn hoặc kép. Sự hiện diện của nó làm cho mã của chúng tôi tương tác nhiều hơn. Nó là một tham số tùy chọn.: The first number in the series. It is an optional parameter.

  • Hàm

    Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
    Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
    Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
    Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
    Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
    
    3: The last number in the series. It is a required parameter.

  • Hàm

    Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
    Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
    Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
    Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
    Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
    
    3 cho phép chúng tôi tự động tạo một loạt các số. Chúng tôi lặp lại loạt trong khi thực hiện một số dòng mã cho đến khi chúng tôi đến số cuối cùng trong chuỗi.: The step size by which we want to increment or decrement our series. It is an optional parameter.

Thông số

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1 Column 5
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2 Column 5
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3 Column 5
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4 Column 5
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5 Column 5
1: Số đầu tiên trong loạt. Nó là một tham số tùy chọn.

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1 Column 5
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2 Column 5
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3 Column 5
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4 Column 5
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5 Column 5
2: Số cuối cùng trong loạt. Nó là một tham số bắt buộc.

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1 Column 5
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2 Column 5
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3 Column 5
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4 Column 5
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5 Column 5
3: Kích thước bước mà chúng tôi muốn tăng hoặc giảm chuỗi của chúng tôi. Nó là một tham số tùy chọn.

Cú pháp

# for loop
for x in series:
   Do something

# while loop
while condition is met:
    Do something

Tham số

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
8: Một chuỗi được bao quanh trong các trình điều khiển đơn hoặc kép. Sự hiện diện của nó làm cho mã của chúng tôi tương tác nhiều hơn. Nó là một tham số tùy chọn.

Hàm

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
3

  1. Hàm
    Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
    Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
    Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
    Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
    Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
    
    3 cho phép chúng tôi tự động tạo một loạt các số. Chúng tôi lặp lại loạt trong khi thực hiện một số dòng mã cho đến khi chúng tôi đến số cuối cùng trong chuỗi.
  2. Thông số
  3. Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1 Column 5
    Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2 Column 5
    Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3 Column 5
    Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4 Column 5
    Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5 Column 5
    
    1: Số đầu tiên trong loạt. Nó là một tham số tùy chọn.
  4. Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1 Column 5
    Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2 Column 5
    Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3 Column 5
    Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4 Column 5
    Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5 Column 5
    
    2: Số cuối cùng trong loạt. Nó là một tham số bắt buộc.

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1 Column 5
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2 Column 5
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3 Column 5
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4 Column 5
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5 Column 5
3: Kích thước bước mà chúng tôi muốn tăng hoặc giảm chuỗi của chúng tôi. Nó là một tham số tùy chọn.

Lưu ý: Nếu chỉ có một tham số được chỉ định, tham số đó được phân loại là tham số

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1 Column 5
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2 Column 5
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3 Column 5
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4 Column 5
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5 Column 5
2, trong khi các tham số
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1 Column 5
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2 Column 5
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3 Column 5
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4 Column 5
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5 Column 5
1 và
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1 Column 5
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2 Column 5
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3 Column 5
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4 Column 5
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5 Column 5
3 được lấy lần lượt là
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1 Column 5
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2 Column 5
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3 Column 5
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4 Column 5
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5 Column 5
7 và
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1 Column 5
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2 Column 5
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3 Column 5
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4 Column 5
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5 Column 5
8.

ourNum = int[input["Enter the number you want to generate a multiplication table for, then hit the `enter` key: "]]

ourRange = range[1,6]

for x in ourRange:

result = ourNum * x

print[ourNum," * ",x," = ",result]

Vòng lặp

Các vòng lặp rất hữu ích khi chúng tôi muốn thực thi một dòng hoặc khối mã nhiều lần, với điều kiện được đáp ứng hoặc cho đến khi chúng tôi đạt được giá trị cuối cùng trong một chuỗi.

Chúng tôi sẽ sử dụng các vòng lặp

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
2 và
# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]
0 trong nhiệm vụ này. Có một sự khác biệt nhỏ giữa cú pháp vòng lặp
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
2 và
# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]
0. Vòng lặp
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
2 hoạt động trong một phạm vi và vòng lặp
# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]
0 chỉ hoạt động khi một hoặc nhiều điều kiện được đáp ứng.

  • Tạo bảng nhân

  • Chúng tôi sẽ tạo bảng nhân của chúng tôi dựa trên sơ đồ dưới đây:

  • Sơ đồ của chúng tôi ở trên chuyển thành thuật toán sau:

  • Bắt đầu chương trình.

  • Nhận đầu vào số nguyên từ người dùng.

Tiếp theo, chúng tôi sẽ xác định một điều kiện
# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]
5 hoặc một điều kiện.
# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]
5 sẽ được sử dụng trong vòng lặp
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
2 và điều kiện sẽ được sử dụng trong vòng lặp
# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]
0.

Enter the number you want to generate a multiplication table for, then hit the enter key:7 
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35

Cuối cùng, mã sẽ kiểm tra mục hoặc điều kiện của chúng tôi. Mã trong vòng lặp sẽ tiếp tục thực thi cho đến khi các mục nằm ngoài phạm vi hoặc điều kiện không được đáp ứng.

Mã sử ​​dụng vòng lặp
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
2

Chúng tôi sẽ sử dụng mã sau để tạo bảng nhân bằng cách sử dụng vòng lặp

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
2.

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
0

Bảng nhân bằng cách sử dụng vòng lặp cho vòng lặp

Các vòng lặp rất hữu ích khi chúng tôi muốn thực thi một dòng hoặc khối mã nhiều lần, với điều kiện được đáp ứng hoặc cho đến khi chúng tôi đạt được giá trị cuối cùng trong một chuỗi.

Chúng tôi sẽ sử dụng các vòng lặp

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
2 và
# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]
0 trong nhiệm vụ này. Có một sự khác biệt nhỏ giữa cú pháp vòng lặp
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
2 và
# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]
0. Vòng lặp
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
2 hoạt động trong một phạm vi và vòng lặp
# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]
0 chỉ hoạt động khi một hoặc nhiều điều kiện được đáp ứng.

  • Tạo bảng nhân

  • Chúng tôi sẽ tạo bảng nhân của chúng tôi dựa trên sơ đồ dưới đây:

  • Sơ đồ của chúng tôi ở trên chuyển thành thuật toán sau:

  • Bắt đầu chương trình.

  • Nhận đầu vào số nguyên từ người dùng.

  • Tiếp theo, chúng tôi sẽ xác định một điều kiện

    # Multiplication table [from 1 to 10] in Python
    
    num = 12
    
    # To take input from the user
    # num = int[input["Display multiplication table of? "]]
    
    # Iterate 10 times from i = 1 to 10
    for i in range[1, 11]:
       print[num, 'x', i, '=', num*i]
    
    5 hoặc một điều kiện.
    # Multiplication table [from 1 to 10] in Python
    
    num = 12
    
    # To take input from the user
    # num = int[input["Display multiplication table of? "]]
    
    # Iterate 10 times from i = 1 to 10
    for i in range[1, 11]:
       print[num, 'x', i, '=', num*i]
    
    5 sẽ được sử dụng trong vòng lặp
    Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
    Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
    Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
    Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
    Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
    
    2 và điều kiện sẽ được sử dụng trong vòng lặp
    # Multiplication table [from 1 to 10] in Python
    
    num = 12
    
    # To take input from the user
    # num = int[input["Display multiplication table of? "]]
    
    # Iterate 10 times from i = 1 to 10
    for i in range[1, 11]:
       print[num, 'x', i, '=', num*i]
    
    0.

Tiếp theo, chúng tôi sẽ xác định một điều kiện
# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]
5 hoặc một điều kiện.
# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]
5 sẽ được sử dụng trong vòng lặp
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
2 và điều kiện sẽ được sử dụng trong vòng lặp
# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]
0.

Enter the number you want to generate a multiplication table for, then hit the enter key:7 
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35

Cuối cùng, mã sẽ kiểm tra mục hoặc điều kiện của chúng tôi. Mã trong vòng lặp sẽ tiếp tục thực thi cho đến khi các mục nằm ngoài phạm vi hoặc điều kiện không được đáp ứng.

Người đóng góp

Goodness Chidinma Abarugo

Làm thế nào để bạn tạo một bảng tùy chỉnh trong Python?

Làm thế nào để dễ dàng tạo bảng trong Python..
Cài đặt bảng. Trước tiên chúng tôi cài đặt thư viện Tabulation bằng cách cài đặt PIP trong dòng lệnh: PIP Cài đặt Tabulation ..
Nhập chức năng bảng. ....
Danh sách danh sách. ....
Từ điển của Iterables. ....
giá trị bị mất..

Làm thế nào để bạn chèn một bàn trong Python?

Tạo một bảng bằng Python..
Thiết lập kết nối với cơ sở dữ liệu bằng phương thức Connect [] ..
Tạo một đối tượng con trỏ bằng cách gọi phương thức con trỏ [] trên đối tượng kết nối được tạo ở trên ..
Bây giờ thực thi câu lệnh CREATE TABLE bằng phương thức Execute [] của lớp con trỏ ..

Làm thế nào để bạn in một bảng được lồng cho bảng vòng trong Python?

Chương trình sau đây sử dụng vòng lặp lồng nhau để hiển thị các bảng nhân từ 1-10.Chức năng PRINT [] Vòng bên trong có kết thúc = '' có liên quan đến một không gian thay vì dòng mới mặc định.Do đó, các số sẽ xuất hiện trong một hàng.Bản in cuối cùng [] sẽ được thực hiện ở cuối bên trong cho vòng lặp.The print[] function inner loop has end=' ' which appends a space instead of default newline. Hence, the numbers will appear in one row. Last print[] will be executed at the end of inner for loop.

Làm thế nào để bạn lưu trữ dữ liệu trong một vòng lặp trong Python?

Giải pháp được cung cấp thực hiện như sau:..
Tạo một danh sách trống được gọi là my_list ..
Mở một vòng lặp cho biến được khai báo "Xin chào" trong các trích dẫn ..
Sử dụng từ khóa Char làm biến vòng lặp.....
Sử dụng thuộc tính Phụ lục được tích hợp trong tất cả Danh sách Python để thêm char ở cuối danh sách ..
Khi vòng lặp kết thúc, danh sách cuối cùng được in ..

Bài Viết Liên Quan

Chủ Đề