My program doesn't check if there's a duplicate inside a string correctly
<p>I have to do a program that check if there's a duplicate digit inside a 4 number string.
The problem is that like 80% of the times the program classifies as not valid also number which aren't wrong.</p>

<pre><code>import random
def extraction():
    random_number = str(random.randint(1000,9999))
    print(random_number)
    for number in random_number:
        n = 0
        print("number1 ",number)
        for number2 in random_number:
            print("number2 ",number2)
            if(number == number2):
                n = n + 1
                print("n ",n)
                if(n == 2):
                    print("The extracted number is not valid.\n")
                    extraction()
</code></pre>

<p>Why don't you make a <code>set()</code> out of the number string and evaluate the length of the set? If there are duplicate digits, only one entry will be stored in the set</p>

<pre><code>if len(set(str(random_number))) == 4:
   pass #you have 4 unique digits
</code></pre>

<p>Example of working principle:</p>

<pre>>>> set('1234')
set(['1', '3', '2', '4'])
>>> set('1123')
set(['1', '3', '2'])</pre>

python