Home > OS >  How to randomly generate and test if an integer is unique on a list and repeat if it is not
How to randomly generate and test if an integer is unique on a list and repeat if it is not

Time:10-20

I am trying to make a program to learn binary. It would work as follows: the program asks me to convert from decimal to binary between 0 and 15, once this conversion is done the decimal digit is removed and must not reappear in one of the next questions. Thus, the 16 digits will be asked one and only one time in the disorder.

To do this, I randomly generate a number to convert and then check if it has already been used, and therein lies my problem: I am supposed to test in a list if the random number has already appeared and if so, generate a new one until it is unique, except that I don't succeed, even if it already exists the number is accepted and the question is asked instead of generating another one.

import random

def conversion(binaire):
    if binaire=="0000":
        decimal="0"
    elif binaire=="0001":
        decimal="1"
    elif binaire=="0010":
        decimal="2"
    elif binaire=="0011":
        decimal="3"
    elif binaire=="0100":
        decimal="4"
    elif binaire=="0101":
        decimal="5"
    elif binaire=="0110":
        decimal="6"
    elif binaire=="0111":
        decimal="7"
    elif binaire=="1000":
        decimal="8"
    elif binaire=="1001":
        decimal="9"
    elif binaire=="1010":
        decimal="10"
    elif binaire=="1011":
        decimal="11"
    elif binaire=="1100":
        decimal="12"
    elif binaire=="1101":
        decimal="13"
    elif binaire=="1110":
        decimal="14"
    elif binaire=="1111":
        decimal="15"
    return decimal
        
    
def existe(liste, n):
    if n in liste:
        return 1
    else:
        return 0

def apprendre_binaire():
    remain = 15
    binaire = ["0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"]
    binaire_deja_fait = []
    remain = 15
    print("Début du programme d'entrainement\n")
    while remain >=0:
        continuer=1
        number = random.randint(0,15)

#This part is supposed to test if number is unique or not

        while continuer==1 :
            test = existe(binaire_deja_fait, number)
            if test == 1:
                number = random.randint(0,15)
            else:
                continuer = 0
                    
        print("Convertir", number, "en binaire :\n")
        answer = input()
        if answer == binaire[number]:
            print("Juste\n")
        else:
            print("Faux, seconde chance :\n")
            answer = input()
            if answer == binaire[number]:
                print("Juste\n")
            else:
                print("Faux, la bonne réponse était", binaire[number], "\n")
                
        add=conversion(binaire[number])
        binaire_deja_fait.append(add)
        print(binaire_deja_fait)
        remain-=1
    print("Fin du programme d'entrainement")
        
apprendre_binaire()

This is a project to help me in my studies, I am aware that it is not optimal.

CodePudding user response:

The problem stems from the fact that you are appending strings to binaire_deja_fait and asking if it contains integer.

You can fix it by changing existe to:

def existe(liste, n):
    if str(n) in liste:
        return 1
    else:
        return 0

I absolutely hate the existance of the existe function, tho. Instead of:

    while continuer==1 :
        test = existe(binaire_deja_fait, number)
        if test == 1:
           number = random.randint(0,15)
        else:
           continuer = 0

I would do :

    while str(number) in binaire_deja_fait:
        number = random.randint(0,15)

and removed the existe function.

The other thing is that your program with enter the endless loop once all numbers are exhausted.

  • Related