Khi sử dụng một câu lệnh ghép trong Python [các câu lệnh cần bộ, một khối thụt lề] và khối đó chỉ chứa các câu lệnh đơn giản, bạn có thể xóa dòng mới và tách các câu lệnh đơn giản bằng dấu chấm phẩy.
Tuy nhiên, điều đó không hỗ trợ các tuyên bố hỗn hợp.
So:
if expression: print "something"
hoạt động, nhưng
while expression: if expression: print "something"
Không phải vì cả hai tuyên bố while
và if
đều hợp chất.
Đối với ví dụ cụ thể của bạn, bạn có thể thay thế phần if expression: assignment
bằng biểu thức có điều kiện, do đó bằng cách sử dụng biểu thức thay vì câu lệnh phức tạp:
while expression: target = true_expression if test_expression else false_expression
Nói chung, hoặc while n 0: a = a - 1; print[a]
Bài học này bao gồm khả năng viết một dòng ____ 4 vòng lặp. Nó cũng bao gồm những hạn chế của phương pháp này. Bạn sẽ tìm thấy ví dụ được sử dụng trong video này dưới đây.
All right. So now that we’ve seen how complicated some while
loops can be by nesting,
let’s look at how simple they can be by just looking at a single-line while
loop.
00:00 Được rồi. Vì vậy, bây giờ chúng tôi đã thấy một số vòng lặp while
có thể phức tạp như thế nào bằng cách làm tổ, hãy để Lôi nhìn vào mức độ đơn giản của chúng bằng cách chỉ nhìn vào một vòng lặp while
một dòng.
We’re going to have a variable
while expression: if expression: print "something"
1. It’s going to be equal to while expression: if expression: print "something"
2. And now a single-lined while
loop starts off the same way, with a while
,
and then it has our condition that needs to be met. 00:11 Chúng tôi sẽ có một biến
1. Nó sẽ bằng while expression: if expression: print "something"
2. Và bây giờ, một vòng lặp while expression: if expression: print "something"
while
duy nhất bắt đầu theo cùng một cách, với một while
, và sau đó nó có điều kiện của chúng tôi cần được đáp ứng. So let’s say
that
while expression: if expression: print "something"
1 needs to be greater than while expression: if expression: print "something"
6. And then you’re still going to have your colon [while expression: if expression: print "something"
7] here. And at that point, you just continue to type out what you want to
happen over each iteration. 00:23 Vì vậy, hãy để nói rằng
1 cần phải lớn hơn while expression: if expression: print "something"
6. Và sau đó, bạn vẫn sẽ có đại tràng [while expression: if expression: print "something"
7] ở đây. Và tại thời điểm đó, bạn chỉ tiếp tục gõ những gì bạn muốn xảy ra trong mỗi lần lặp. So first, I’m going to take while expression: if expression: print "something"
while expression: if expression: print "something"
1 and I’m going to add 00:38 Vì vậy, trước tiên, tôi sẽ lấy
1 và tôi sẽ thêm
while expression: if expression: print "something"
while expression: if expression: print "something"
9. And then your second statement is going to be separated with a semicolon [while expression: target = true_expression if test_expression else false_expression
0].
So my second thing I want it to do is to while expression: target = true_expression if test_expression else false_expression
1. 00:45
9. Và sau đó tuyên bố thứ hai của bạn sẽ được phân tách bằng dấu chấm phẩy [while expression: if expression: print "something"
0]. Vì vậy, điều thứ hai của tôi tôi muốn nó làm là while expression: target = true_expression if test_expression else false_expression
1. Oop, ha. This
should be a minus [while expression: target = true_expression if test_expression else false_expression
while expression: target = true_expression if test_expression else false_expression
2], because that would go on forever. 00:58 OOP, ha. Đây phải là một điểm trừ [
2], bởi vì điều đó sẽ diễn ra mãi mãi. All right, and that’s it! Let’s see what happens.
Here we go! So my while expression: target = true_expression if test_expression else false_expression
while
loop still works but I have it all on one line. Now, one thing I do want to point
out—I cannot have a nested statement in a single-line while
loop.
01:06 Được rồi, và đó là nó! Hãy để xem những gì xảy ra. Ở đây chúng tôi đi! Vì vậy, vòng lặp while
của tôi vẫn hoạt động nhưng tôi có tất cả trên một dòng. Bây giờ, một điều tôi muốn chỉ ra rằng tôi không thể có một câu lệnh lồng nhau trong vòng lặp while
một dòng.
So I couldn’t say if
, you know,
while expression: target = true_expression if test_expression else false_expression
6 and then have another thing that says while expression: target = true_expression if test_expression else false_expression
1.01:44 Đây không phải là đi làm. Vì vậy, khi tôi cố gắng chạy điều đó, tôi gặp lỗi và đó là bởi vì tôi có điều này, bạn có thể đặt câu lệnh có điều kiện lồng nhau này trong một khối một dòng. This isn’t going to work. So when I try to run that, I get an error, and that is because I have this—you can’t put this nested conditional statement in a single-line block.
01:58 Nhưng được rồi! Điều đó khá nhiều kết thúc hướng dẫn của chúng tôi về các vòng while
. Nếu bạn muốn tìm hiểu thêm thông tin, bạn luôn có thể truy cập các tài nguyên của chúng tôi trên Python thực sự, nhưng tôi hy vọng bạn có một số thông tin tốt và tìm hiểu tất cả về các vòng lặp while
!
But all right! That pretty much wraps up our tutorial on while
loops. If you would like to find out more information,
you can always visit our resources on Real Python, but I hope you got some good information and learned all about while
loops!