Bootstrap

如何让程序识别输入的是中文/英文

算法:

def detect_language(input_str):
    english_count = 0
    chinese_count = 0

    for char in input_str:
        if 'a' <= char <= 'z' or 'A' <= char <= 'Z':
            english_count += 1
        elif '\u4e00' <= char <= '\u9fff':
            chinese_count += 1

    if english_count > chinese_count:
        return "English"
    else:
        return "Chinese"

-源代码~~

def detect_language(input_str):
    english_count = 0
    chinese_count = 0

    for char in input_str:
        if 'a' <= char <= 'z' or 'A' <= char <= 'Z':
            english_count += 1
        elif '\u4e00' <= char <= '\u9fff':
            chinese_count += 1

    if english_count > chinese_count:
        return "English"
    else:
        return "Chinese"

input_str = "你好,这是一个测试"
print(detect_language(input_str))  # 输出: Chinese

input_str = "Hello, this is a test"
print(detect_language(input_str))  # 输出: English

;