1
stop = False
text_count = 0
while stop is False:
    int_string = input("What is the initial string? ")
    int_string = int_string.lower()
    for a in int_string:
        for a in b:
            text_count += 1
    if text_count < 3:
        print("Error: string must have at least 3 characters.")
    elif len(int_string) != 'X' or len(int_string) != 'x' or len(int_string) != '_':
        print("Error: string must contain only X and _.")

此字符串必须至少有 3 个字符长,并且必须仅包含字符“X”、“x”或“_”(下划线)也不区分大小写

4

2 回答 2

2

您可以将其转换为set对象并将其与您允许的字符集进行比较:

while not stop:
    int_string = input("What is the initial string? ").lower()

    if len(int_string) < 3:
        print("Error: string must have at least 3 characters.")
    elif set(int_string) <= {'x', '_'}:
        print("Error: string must contain only X and _.")

a <= b是 的简写a.issubset(b)

于 2013-03-14T02:15:58.137 回答
1

这是另一种选择:

while not stop:
    int_string = raw_input("What is the initial string? ").lower()   
    if len(int_string) < 3:
        print("Error: string must have at least 3 characters.")
    elif any(ch not in ('x', '_') for ch in set(int_string)):
        print("Error: string must contain only X and _.")
于 2013-03-14T02:21:40.410 回答