Cách kết thúc mã trong python

Các hàm quit[], exit[], sys. thoát [] và hệ điều hành. _exit[] có chức năng gần như giống nhau khi chúng tăng ngoại lệ SystemExit theo đó trình thông dịch Python thoát và không có dấu vết ngăn xếp nào được in. Chúng tôi có thể bắt ngoại lệ để chặn các lần thoát sớm và thực hiện các hoạt động dọn dẹp; .  

hàm thoát [] trong Python

Hàm bỏ [] chỉ hoạt động nếu mô-đun trang được nhập, vì vậy không nên sử dụng hàm này trong mã sản xuất. Mã sản xuất có nghĩa là mã đang được sử dụng bởi đối tượng dự định trong tình huống thực tế. Chức năng này chỉ nên được sử dụng trong trình thông dịch. Nó làm tăng ngoại lệ SystemExit đằng sau hậu trường. Nếu bạn in nó, nó sẽ đưa ra một thông báo

Ví dụ.  

Python3




0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
62

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
63

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
64
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
65
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
66
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
67
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
0
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
1
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
2

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
5

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____07

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3______09

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
11
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
65
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
13
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
13
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
15
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
16

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
17

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
17
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
19

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
17
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
01
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
02

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
17
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
04

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____201____207

đầu ra

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
7

hàm thoát [] trong Python

Lối ra [] được xác định trong trang web. py và nó chỉ hoạt động nếu mô-đun trang web được nhập, vì vậy nó chỉ được sử dụng trong trình thông dịch. Nó giống như một từ đồng nghĩa với từ bỏ [] để làm cho Python thân thiện hơn với người dùng. Nó cũng đưa ra một thông điệp khi được in.  

Ví dụ

Python3




0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
62

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
09

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
64
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
65
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
66
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
67
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
0
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
1
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
2

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
5

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____07

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____1632

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
11
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
65
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
13
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
13
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
15
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
16

 

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
17
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
641

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
17
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
01
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
644

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
17
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
646

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____201____207

đầu ra

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit

hệ thống. thoát [[arg]] bằng Python

Không giống như thoát[] và thoát[], sys. exit[] được coi là tốt để sử dụng trong mã sản xuất cho mô-đun sys luôn có sẵn. Đối số tùy chọn arg có thể là một số nguyên đưa ra lối ra hoặc một loại đối tượng khác. Nếu là số nguyên, số 0 được coi là “kết thúc thành công”.
Lưu ý. Một chuỗi cũng có thể được chuyển đến sys. phương thức thoát [].

Ví dụ.  

Python3




0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
62

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
651

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
652
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
653

 

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
654
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
13
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
656

 

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
657

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
11
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
659____1660
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
661

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____1663

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____1665
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
666
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
667

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
668
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
16

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____201____00
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
673
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
674

đầu ra

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
1

hệ điều hành. _exit[n] trong Python

hệ điều hành. Phương thức _exit[] trong Python được sử dụng để thoát khỏi quy trình với trạng thái đã chỉ định mà không gọi trình xử lý dọn dẹp, xóa bộ đệm stdio, v.v.  

Ghi chú. Phương pháp này thường được sử dụng trong tiến trình con sau os. lệnh gọi hệ thống fork[]. Cách tiêu chuẩn để thoát khỏi quy trình là sys. phương thức thoát [n]

Python3




0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
675

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
677

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
652
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
679

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
01

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
02

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
03
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
13
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
05

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
08

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
09

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
11
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
11____012
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
16

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____201____00
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
18
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
674

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
21

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____023

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
25

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
27

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
29

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3______031
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
13
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
33
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
12
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
674

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
17

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
39

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____041

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____043

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
45

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
48

_______03____050

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
52

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
17

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____055

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
57

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____111
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
60____061
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
62

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
17
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
64
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
13
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
66
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
61
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
68

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
17
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
01
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
0
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
72
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
73

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
668
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
16

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____201____00
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
80
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
674

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____201____00
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
85
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
86

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____201____00
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
90
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
674

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____201____00
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
95
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
674

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
17

_______03____099

_______03____1101

_______03____1103

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3____1105

đầu ra

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
0

Phần kết luận. Trong số bốn chức năng thoát trên, sys. exit[] được ưu tiên chủ yếu vì các hàm exit[] và thoát[] không thể được sử dụng trong mã sản xuất trong khi hệ điều hành. _exit[] chỉ dành cho các trường hợp đặc biệt khi cần thoát ngay lập tức

Có chương trình kết thúc bằng Python không?

Các hàm thoát[] và thoát[] . Chúng ta cũng có thể sử dụng hàm exit[] trong trình bao tương tác Python để kết thúc thực thi chương trình và chọn không tham gia trình bao tương tác Python như hình bên dưới. The exit[] function is a cross-platforms function that is used to terminate program execution in Python. We can also use the exit[] function in the Python interactive shell to end program execution and opt out of the Python interactive shell as shown below.

Điều gì diễn ra ở cuối mã Python?

Không giống như các ngôn ngữ khác, Python không sử dụng ký tự cuối dòng . Hầu hết thời gian một Enter đơn giản sẽ làm.

Chủ Đề