0

This code must be able to identify numbers that have already been used, so they are not printed again.

def Bingo():                    
    import random
    import time

    while True:
        try:

            command = input("Do you want to generate a number Y/N: ")
        except:
            print("You colossal spanner")

        if command.lower() == "y":

            text_file = open("Cache.txt", "a")

            text_file.write("Numbers Used \n")

            print("Welcome to the Bingo number generator")

            UserNumber = str((random.randint(1,90)))

            print(UserNumber)

            text_file.write (UserNumber)

            text_file.close()

        elif command.lower == "N":
            print("Try generating another one: ")

Bingo()

text_file.close()
idjaw
  • 25,487
  • 7
  • 64
  • 83
B.Khamis
  • 31
  • 6
  • 1
    I'm pretty sure this is a duplicate. The standard solution is to generate a random shuffle of the range (or a random selection ot ouf a range) and yield one element at a time from the shuffled list. – tripleee Mar 11 '16 at 13:17

3 Answers3

3

You can make a list of all available numbers and remove already generated numbers from it. Instead of the generation of the numbers, you would generate indexes in the list of those numbers. That would make the generation deterministic:

numbers = list(range(1, 90))

def Bingo():
    import random
    import time

    while True:
        try:
            command = input("Do you want to generate a number Y/N: ")
        except:
            print("You colossal spanner")
        if command.lower() == "y":
            text_file = open("Cache.txt", "a")
            text_file.write("Numbers Used \n")
            print("Welcome to the Bingo number generator")

            remaining_numbers = len(numbers)
            if remaining_numbers == 0:
                print('No more numbers')
                exit(-1)
            index = random.randrange(remaining_numbers)
            user_number = numbers.pop(index)
            print(user_number)
            text_file.write (str(user_number))
            text_file.close()

        elif command.lower == "N":
            print("Try generating another one: ")

Bingo()

text_file.close()
Grief
  • 1,839
  • 1
  • 21
  • 40
1

I suggest you to generate your numbers like this:

numbers = range(1,90)

then in the bingo do somthing like this to get the next number:

number = random.randrange(len(numbers))
v = numbers.pop(number)
print v

You can check if other numbers are availble to be extracted checking the length of numbers like this:

len(numbers) > 0

Generate the numbers inside your function, so that everytime you call Bingo() you have a new list of numbers to use during the game.

pdepmcp
  • 136
  • 1
  • 4
0

You need to make a list of the used numbers, and when you say UserNumber = str((random.randint(1,90))), put it in a while loop, and add it to the list of numbers:

def Bingo():                    
    import random
    import time

    used = []
    while True:
        try:
            command = input("Do you want to generate a number Y/N: ")
        except:
            print("You colossal spanner")

        if command.lower() == "y":
            text_file = open("Cache.txt", "a")
            text_file.write("Numbers Used \n")

            print("Welcome to the Bingo number generator")

            UserNumber = None
            while UserNumber is None or UserNumber in used:
                UserNumber = str((random.randint(1,90)))

            used.append(UserNumber)
            print(UserNumber)
            text_file.write (UserNumber)
            text_file.close()

        elif command.lower == "N":
            print("Try generating another one: ")

Bingo()

text_file.close()
zondo
  • 19,901
  • 8
  • 44
  • 83