The loop that never ends · ลูปที่ไม่เคยจบ
The loop that never ends
Run this first and watch what happens. The line that makes count smaller is there — but it is outside the loop, so it never runs.
In Python the indentation is the structure: lines indented under while belong to the loop and repeat, a line back at the left margin comes after the loop and runs once at the end. Here the loop never reaches its last line, so its question stays true for ever. The page will notice and stop it for you, but your program should stop itself.
ลูปที่ไม่เคยจบ
รันโค้ดนี้ก่อนแล้วดู会发生什么 บรรทัดที่ทำให้ count ลดลงมีอยู่ — แต่มันอยู่ นอก ลูป จึงไม่มีการรัน
ใน Python การจัดย่อ คือ โครงสร้าง: บรรทัดที่ถูกจัดย่อใต้ while เป็นส่วนหนึ่งของลูปและซ้ำซ้อน บรรทัดที่กลับไปที่ขอบซ้ายจะมาจากหลังลูปและรันหนึ่งครั้งสุดท้ายที่นี่ ลูปไม่เคยถึงบรรทัดสุดท้าย ทำให้คำถามของมันเป็นจริงตลอดหน้าเว็บจะ noticing และหยุดให้ แต่โปรแกรมของคุณควรหยุดเอง
Run this first and watch what happens. The line that makes count smaller is there — but it is outside the loop, so it never runs. Move it inside so the program prints 5, 4, 3, 2, 1 and stops. · รันสิ่งนี้ก่อนเพื่อดูว่าเกิดอะไรขึ้น. บรรทัดที่ทำให้ count เล็กลงมีอยู่ — แต่มันอยู่ นอก ลูป ดังนั้นมันจะไม่被执行. ย้ายมันเข้าไปภายในเพื่อให้โปรแกรมพิมพ์ 5, 4, 3, 2, 1 และหยุด
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่