4

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 random number, print "correct".

I am using this code for the random number

num = random.randint (0,99999)
print (num)

The problem is it will not always print a five digit number.

Also, I don't know how to match the user number with the random number.

Thank you.

Mark Dickinson
  • 29,088
  • 9
  • 83
  • 120

4 Answers4

12

Take a random sample of the digits 0 to 9:

''.join(random.sample('0123456789', 5))
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • He asked for no repeating digits. He didnt ask for no duplicate digits. Presumably this means "121" is acceptable but "112" is not. Though we would have to ask the OP to be sure. – Jeffrey Phillips Freeman Apr 29 '18 at 21:11
  • @JeffreyPhillipsFreeman - that would be "no sequentially repeating digits". Its worth requesting clarification from OP in the question. But I don't see the value of simply asserting that here. – tdelaney Apr 29 '18 at 21:16
  • the value of asserting it here is in the hopes you go to the OP to follow up. I havent submitted any answers so I personally dont need clarification. I did however upvote your answer regardless. – Jeffrey Phillips Freeman Apr 29 '18 at 21:18
  • @JeffreyPhillipsFreeman - That would just make me a third party to your question. I've learned to avoid triangles! – tdelaney Apr 29 '18 at 21:22
  • its the squares you really have to worry about. They never know how to have any fun! – Jeffrey Phillips Freeman Apr 29 '18 at 21:33
  • Or instead of having one long string you can use `range()` `random.sample(range(0,10),5)` – Sergiy Kolodyazhnyy Sep 22 '18 at 18:02
0

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)
JacobIRR
  • 8,545
  • 8
  • 39
  • 68
  • 2
    OP says there can't be repeating digits, so zero-filling would violate that, wouldn't it? – Random Davis Apr 11 '17 at 16:29
  • Ah you're right. Fixed. This solution uses recursion so the function calls itself again if there were repeating digits in the random number. – JacobIRR Apr 11 '17 at 16:43
0

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)

Community
  • 1
  • 1
dawg
  • 98,345
  • 23
  • 131
  • 206
0

If you generate your numbers like this:

larger_number = ''.join(random.sample(string.digits, 5))

And got the numbers from the user like this:

def get_user_num(length=5):
    while True:
        num = raw_input('Enter a {}-digit number with no repeating digits: '.format(length)).zfill(length)
        if len(set(num)) < length:
            print('Please try again.')
            continue
        else:
            return num

You could determine if the user's numbers were in the number list like so:

set(user_number) < set(larger_number)

And then it would be a really simple matter to combine this all together into a program. Note that the numbers are never actually treated as numbers - they're just strings.

Random Davis
  • 6,662
  • 4
  • 14
  • 24