Python - 5 digit random number generator with no repeating digits

前端 未结 4 1151
臣服心动
臣服心动 2021-01-06 15:25

I am supposed to print a random 5-digit number with no repeating digits, then ask the user for a three digit number. If the user\'s number contains three digits from the ran

4条回答
  •  清歌不尽
    2021-01-06 15:46

    You can generate all the 5 digits ints with unique digits like so:

    tgt=set()
    for i in range(1234,99999+1):
        s='{:05d}'.format(i)
        if len(set(s))==5:
            tgt.add(s)  
    

    Then use random.choose(tgt) to select one at random.

    (but tdelaney's answer is better)

提交回复
热议问题