Python - 5 digit random number generator with no repeating digits

前端 未结 4 1120
臣服心动
臣服心动 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条回答
  •  旧时难觅i
    2021-01-06 15:30

    Use zfill and set like so: Edited to account for numbers with repeating digits

    import random
    
    
    def threeInFive(user_num):
        num = str(random.randint(0, 99999)).zfill(5)
        num = ''.join(set([n for n in num]))
        if len(num) < 5:
            print "There were repeating digits...trying again"
            threeInFive(user_num)
        elif str(user_num) in num:
            print "The user number is in there!"
            return True
        else:
            print "The user number : %s is not in : %s" % (user_num, num)
            return False
    
    threeInFive(500)
    

提交回复
热议问题