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
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
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ố
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.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
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
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
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ố
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.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
Hàm
3: The last number in the series. It is a required parameter.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
Hàm
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.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
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
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- Hàm
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
- Thông số
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
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.
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
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.
# 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]
# 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]
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
# 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]
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
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
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
0Bả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
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# 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]
2 và điều kiện sẽ được sử dụng trong vòng lặpRow 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.# 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]
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.
# 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]
# 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]
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
# 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]
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