Hướng dẫn write a program to print color name if user enters the first letter of the color name in python - viết chương trình in tên màu nếu người dùng nhập ký tự đầu tiên của tên màu trong python

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:51:50 (UTC/GMT +8 giờ)

Python Basic: Tập thể dục-8 với giải pháp

Viết một chương trình Python để hiển thị các màu đầu tiên và cuối cùng từ danh sách sau.

color_list = ["màu đỏ", "màu xanh lá cây", "trắng", "đen"]]]

Trình bày bằng hình ảnh:

Hướng dẫn write a program to print color name if user enters the first letter of the color name in python - viết chương trình in tên màu nếu người dùng nhập ký tự đầu tiên của tên màu trong python

Giải pháp mẫu:-:-

Mã Python:

color_list = ["Red","Green","White" ,"Black"]
print( "%s %s"%(color_list[0],color_list[-1]))

Đầu ra mẫu:

Red Black

Flowchart:

Hướng dẫn write a program to print color name if user enters the first letter of the color name in python - viết chương trình in tên màu nếu người dùng nhập ký tự đầu tiên của tên màu trong python

Trực quan hóa thực thi mã Python:

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

Trình chỉnh sửa mã Python:

Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn (và nhận xét) thông qua Disqus.

Trước đây: Viết chương trình Python để chấp nhận tên tệp từ người dùng và in phần mở rộng của đó. (trích xuất ngày từ exic_st_date). Write a Python program to accept a filename from the user and print the extension of that.
Next: Write a Python program to display the examination schedule. (extract the date from exam_st_date).

Python: Lời khuyên trong ngày

Thử-Catch-Else Construct:

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")

Phân đoạn chương trình được đưa ra dưới đây chấp nhận một ký tự từ bàn phím và in tên của màu tương ứng, e. g., Nếu người dùng nhập ký tự r, nó in màu đỏ. Tuy nhiên, nó chỉ xử lý ba màu, cụ thể là màu đỏ, xanh lá cây và màu xanh lam. Nhân vật được nhập từ bàn phím trước tiên được chấp nhận với màu thay đổi (thuộc loại char) và sau đó được kiểm tra trong câu lệnh Switch. Chương trình bao gồm ba trường hợp tương ứng với màu đỏ, xanh lá cây và xanh dương. Lưu ý rằng các giá trị trường hợp là các hằng số ký tự, R, ’G, và‘ B. Mỗi trường hợp bao gồm một câu lệnh PRINTF để in tên màu theo sau là một câu lệnh break. Đầu ra của chương trình chứa mã trên được hiển thị bên dưới.
The character entered from the keyboard is first accepted in variable color (of type char) and then tested in the switch statement. The program includes three cases corresponding to the colors red, green, and blue. Note that the case values are character constants’ R,’ ‘G’ and ‘B.’ Each case includes a printf statement to print the color’s name followed by a break statement. The output of the program containing the above code shown below.

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}

Hướng dẫn write a program to print color name if user enters the first letter of the color name in python - viết chương trình in tên màu nếu người dùng nhập ký tự đầu tiên của tên màu trong python

Trong bài viết này, chúng tôi sẽ đề cập đến cách in văn bản màu bằng Python bằng một số phương thức để xuất văn bản màu vào thiết bị đầu cuối trong Python. & NBSP;how to print colored text in Python using several methods to output colored text to the terminal in Python. 

Những cách phổ biến nhất để làm điều này là sử dụng:

  • Sử dụng mô -đun Colorama
  • Sử dụng mô -đun TermColor
  • Sử dụng mã ANSI trong Python

Phương pháp 1: In văn bản màu sử dụng mô -đun Colorama

Mô-đun Colorama là in hình nền tảng của văn bản màu sau đó có thể được thực hiện bằng cách sử dụng tốc ký không đổi Colorama cho các chuỗi thoát ANSI: & nbsp;is aCross-platform printing of colored text can then be done using Colorama’s constant shorthand for ANSI escape sequences: 

Ví dụ 1: & NBSP; Chương trình Python để in văn bản màu đỏ với nền màu xanh lá cây.  Python program to print red text with green background.

Python3

from colorama import Fore, Back, Style

print(Fore.RED ____10

Red Black
1
Red Black
2

print

Red Black
4
Red Black
0
Red Black
6
Red Black
2

print

Red Black
9____10
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
1
Red Black
2

print

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
4

print

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
7
Red Black
2

Output:  

Hướng dẫn write a program to print color name if user enters the first letter of the color name in python - viết chương trình in tên màu nếu người dùng nhập ký tự đầu tiên của tên màu trong python

Ví dụ 2: Chương trình Python để in văn bản màu xanh lá cây với nền màu đỏ. Python program to print green text with red background.

Python3

from colorama import

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
2

from

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
4import
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
6

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
7

Các

Output: 

Hướng dẫn write a program to print color name if user enters the first letter of the color name in python - viết chương trình in tên màu nếu người dùng nhập ký tự đầu tiên của tên màu trong python

Phương pháp 2: In văn bản màu sử dụng mô -đun TermColor

Mô -đun TermColor là một mô -đun Python cho định dạng màu ANSII cho đầu ra trong thiết bị đầu cuối. & NBSP; is a python module for ANSII Color formatting for output in the terminal. 

Ví dụ: Chương trình Python để in văn bản và nền màu.Python program to print colored text and background.

Python3

import from7

from

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
4import colorama 1

colorama 2colorama 3 colorama 4from0from1colorama 7colorama 8colorama 3import0__1

printimport6

import7from0from1from2from1from4

Red Black
2

Fore, Back, Style4 Fore, Back, Style5Fore, Back, Style6 Fore, Back, Style7colorama 7____41print0

Red Black
2

print2from0

Red Black
2

print2print6

Red Black
2

print8 print9(Fore.RED 0 (Fore.RED 1

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
______

(Fore.RED 5(Fore.RED 6(Fore.RED 7(Fore.RED 8colorama 3

Red Black
00
Red Black
2

Các

Output: 

Hướng dẫn write a program to print color name if user enters the first letter of the color name in python - viết chương trình in tên màu nếu người dùng nhập ký tự đầu tiên của tên màu trong python
 

Phương pháp 3: In văn bản màu bằng mã ANSI trong Python

Cách phổ biến nhất để in văn bản màu là bằng cách in trực tiếp các chuỗi thoát ANSI. Điều này có thể được phân phối ở các định dạng khác nhau như: & nbsp;

Ví dụ 1: Xây dựng các chức năng để gọi & nbsp;

Chúng ta có thể xây dựng các chức năng để gọi các hàm có tên màu cụ thể để thực hiện chuỗi thoát ANSI có liên quan. Dưới đây là chương trình Python để in văn bản và nền màu

Python3

Fore, Back, Style4

Red Black
15print
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6
Red Black
18
Red Black
19
Red Black
20
Red Black
21

Fore, Back, Style4

Red Black
23print
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6
Red Black
26
Red Black
19
Red Black
20
Red Black
21

Fore, Back, Style4

Red Black
31print
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6
Red Black
34
Red Black
19
Red Black
20
Red Black
21

Fore, Back, Style4

Red Black
39print
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6
Red Black
42
Red Black
19
Red Black
20
Red Black
21

Fore, Back, Style4

Red Black
47print
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6
Red Black
50
Red Black
19
Red Black
20
Red Black
21

Fore, Back, Style4

Red Black
55print
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6
Red Black
58
Red Black
19
Red Black
20
Red Black
21

Fore, Back, Style4

Red Black
63print
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6
Red Black
66
Red Black
19
Red Black
20
Red Black
21

Fore, Back, Style4

Red Black
71print
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6
Red Black
74
Red Black
19
Red Black
20
Red Black
21

Red Black
78
Red Black
79
Red Black
2

Red Black
81
Red Black
82
Red Black
2

Red Black
84
Red Black
85
Red Black
2

Red Black
87
Red Black
88
Red Black
2

Red Black
84
Red Black
85
Red Black
2

Output: 

Hướng dẫn write a program to print color name if user enters the first letter of the color name in python - viết chương trình in tên màu nếu người dùng nhập ký tự đầu tiên của tên màu trong python

 

Ví dụ 2: Xây dựng một lớp màu

Tạo một lớp để phân bổ nền và màu tiền cảnh và gọi chúng. & Nbsp; bên dưới là chương trình Python để in văn bản và nền màu.

Python3

Red Black
93
Red Black
94

Red Black
95colorama 3
Red Black
97

Red Black
98colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
00

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
01colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
03

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
04colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
06

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
07colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
09

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
10
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
11colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
13

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
14
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
15colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
17

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
18
Red Black
93
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
20

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
222223
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
24

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
26colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
28

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
30colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
32

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
34colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
36

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
38colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
40

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
42colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
44

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
46colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
48

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
50colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
52

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
54colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
56

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
58colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
60

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
62colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
64

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
66colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
68

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
70colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
72

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
74colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
76

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
78colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
80

(Fore.RED 5

Red Black
93
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
83

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
22223
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
87

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
26colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
91

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
30colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
95

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
34colorama 3
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
99

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
38colorama 3
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
03

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
42colorama 3
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
07

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
46colorama 3
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
11

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
50colorama 3
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
15

print

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
17
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
18
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
19
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
20
Red Black
2

print

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
23
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
18
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
19
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
20
Red Black
2

Output: 

Hướng dẫn write a program to print color name if user enters the first letter of the color name in python - viết chương trình in tên màu nếu người dùng nhập ký tự đầu tiên của tên màu trong python

Ví dụ 3: Chức năng lặp lại

Chúng ta có thể thiết kế trình tự, chức năng, các chức năng của Iterating & tự tạo và tự tạo;

Python3

Fore, Back, Style4

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
29

(Fore.RED 5print8

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
32(Fore.RED 0 (Fore.RED 1
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
36(Fore.RED 4

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21print8
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
40(Fore.RED 0 (Fore.RED 1
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6______34441____
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
46(Fore.RED 4

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
48
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
49colorama 3
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
51

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
48print8
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
54(Fore.RED 0 (Fore.RED 1
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
58from1
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
60(Fore.RED 4

Is

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
62
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
49
Red Black
0colorama 3
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
77

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
48print
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
86

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
21print
try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")
6
#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
90
Red Black
2

#include
void main() {
 char color;
 clrscr();
 printf("Enter character (R/G/B): ");
 color= getchar();
 switch (color) {
   case 'R':
         printf ("Red") ;
         break;
   case 'G':
         printf("Green");
         break;
   case 'B':
         printf("Blue");
         break;
  }
 getch();
}
92

Output: 

Hướng dẫn write a program to print color name if user enters the first letter of the color name in python - viết chương trình in tên màu nếu người dùng nhập ký tự đầu tiên của tên màu trong python


Làm thế nào để bạn in các chữ màu trong Python?

Làm thế nào để in văn bản màu trong Python..
Nhập Colorama từ Colorama Nhập khẩu in (trước. ....
Nhập SYS từ termColor Nhập màu, văn bản cprint = màu ('Xin chào, thế giới!', '....
in ("\ 033 [1; 32m Văn bản này có màu xanh lá cây sáng \ n").
Từ nhập màu fg in màu (' %s hello World !!! %s' %(fg (1), attr (0))).

Làm thế nào để bạn tìm thấy màu trong Python?

Những cách phổ biến nhất để làm điều này là sử dụng:..
Sử dụng mô -đun Colorama ..
Sử dụng mô -đun TermColor ..
Sử dụng mã ANSI trong Python ..

Làm thế nào để bạn viết màu trong Python?

Để thêm màu sắc và phong cách vào văn bản, bạn nên tạo một lớp gọi là ANSI và bên trong lớp này, hãy khai báo các cấu hình về văn bản và màu sắc với mã ANSI.Các chức năng được sử dụng: Bối cảnh: Cho phép định dạng nền.Chấp nhận mã ANSI trong khoảng từ 40 đến 47, 100 và 107.create a class called ANSI, and inside this class, declare the configurations about the text and color with code ANSI. Functions Used: background: allows background formatting. Accepts ANSI codes between 40 and 47, 100 and 107.

Làm thế nào để bạn thay đổi màu văn bản trong python nhàn rỗi?

Trong IDLE, chọn Tùy chọn> Định cấu hình IDLE, sau đó nhấp vào tab Điểm nổi bật. Bấm vào văn bản bình thường thả xuống và chọn một loại văn bản để thay đổi.Trong ví dụ này, chúng tôi sẽ chọn "Định nghĩa Python" được biết đến trong IdleColors là Color User1 ().select Options > Configure IDLE , then click on the Highlights tab. Click the drop down Normal text and select a type of text to change. In this example we will choose "Python Definitions" which is known in idlecolors as the colour user1() .