Chúng ta có thể đưa đầu vào vào vòng lặp while trong python không?

Trong lập trình, vòng lặp được sử dụng để lặp lại một khối mã. Ví dụ: nếu chúng tôi muốn hiển thị thông báo 100 lần, thì chúng tôi có thể sử dụng vòng lặp. Đây chỉ là một ví dụ đơn giản, chúng ta có thể đạt được nhiều hơn nữa với các vòng lặp

Trong hướng dẫn trước, chúng ta đã tìm hiểu về vòng lặp for trong Python. Sau đây, chúng ta sẽ tìm hiểu về vòng lặp

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0


Vòng lặp

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 trong Python được sử dụng để chạy một mã cụ thể cho đến khi đáp ứng một điều kiện nhất định

Cú pháp của vòng lặp

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 là

while condition:
    # body of while loop

Đây,

  1. Một vòng lặp
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    0 đánh giá
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    4
  2. Nếu
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    4 ước tính thành
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    6, mã bên trong vòng lặp
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    0 được thực thi
  3. # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    4 được đánh giá lại
  4. Quá trình này tiếp tục cho đến khi điều kiện là
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    9
  5. Khi
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    4 đánh giá thành
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    9, vòng lặp dừng lại

Lưu đồ cho Python While Loop

Chúng ta có thể đưa đầu vào vào vòng lặp while trong python không?
Sơ đồ vòng lặp while trong Python

Ví dụ. Python trong khi vòng lặp

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1

đầu ra

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
4

Đây là cách chương trình hoạt động

Điều kiện biến.

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
42Hành động
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
43
____144
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
46 được in.
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
47 được tăng lên 2.
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
48
______144
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
41 được in.
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
47 được tăng lên 3.
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
43
____144
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
46 được in.
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
47 được tăng lên 4.
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
48
____144
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
while condition:
    # body of while loop
41 được in.
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
47 được tăng lên 5.
while condition:
    # body of while loop
43
______144
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
while condition:
    # body of while loop
46 được in.
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
47 được tăng lên 6.
while condition:
    # body of while loop
48
______144
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
9Vòng lặp kết thúc.


ví dụ 2. Python while Loop để hiển thị cấp độ trò chơi

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
4

đầu ra

while condition:
    # body of while loop
4

Trong ví dụ trên, chúng tôi đã sử dụng vòng lặp

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 để kiểm tra mức hiện tại và hiển thị nó trên bàn điều khiển


Vòng lặp vô hạn trong Python

Nếu

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
4 của một vòng lặp luôn là
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6, vòng lặp sẽ chạy vô hạn (cho đến khi bộ nhớ đầy). Ví dụ,

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
9

Trong ví dụ trên,

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
4 luôn là
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6. Do đó, thân vòng lặp sẽ chạy vô hạn lần


Python Vòng lặp While với other

Vòng lặp

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 cũng có thể có khối tùy chọn
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
97

Phần

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
97 được thực thi sau khi
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
4 trong vòng lặp while ước tính thành
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
9. Ví dụ,

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
7

đầu ra

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
8

Ở đây, chúng tôi đã sử dụng biến đếm để in chuỗi

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
71 ba lần

Ở lần lặp thứ tư, điều kiện trong

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 trở thành
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
9. Do đó, phần
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
97 được thực thi