Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

คำสั่งคีย์บอร์ดอะไรที่หยุด Infinite Loop ใน Python?

Loop ใดๆ ถูกสร้างขึ้นเพื่อ Execute จำนวนครั้งที่กำหนด หรือจนกว่าเงื่อนไขจะเป็นจริง อย่างไรก็ตาม หากเงื่อนไขไม่เกิดขึ้น Loop จะทำซ้ำไม่รู้จบ Infinite Loop แบบนี้ต้องหยุดโดยบังคับด้วยการสร้าง Keyboard Interrupt การกด Ctrl+C จะหยุดการ Execute ของ Infinite Loop

>>> while True:
...     print('hello')
hello
hello
hello
hello
hello
hello
Traceback (most recent call last):
  File "<pyshell#18>", line 2, in <module>
    print('hello')
KeyboardInterrupt