Check if a string contains a number

后端 未结 16 1597
無奈伤痛
無奈伤痛 2020-11-22 11:14

Most of the questions I\'ve found are biased on the fact they\'re looking for letters in their numbers, whereas I\'m looking for numbers in what I\'d like to be a numberless

16条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 11:51

    import string
    import random
    n = 10
    
    p = ''
    
    while (string.ascii_uppercase not in p) and (string.ascii_lowercase not in p) and (string.digits not in p):
        for _ in range(n):
            state = random.randint(0, 2)
            if state == 0:
                p = p + chr(random.randint(97, 122))
            elif state == 1:
                p = p + chr(random.randint(65, 90))
            else:
                p = p + str(random.randint(0, 9))
        break
    print(p)
    

    This code generates a sequence with size n which at least contain an uppercase, lowercase, and a digit. By using the while loop, we have guaranteed this event.

提交回复
热议问题