0

I have the following code which randomly generates size amount of numbers.

def createDatabase(size):
    Database=[]
    for j in range(0, size):
        Database.append(randomNumberGenerator(100,Database))  
#randomNumberGenerator is a separate function in my program

def searchDatabase(Database, guess):
    if(guess in Database):
        print('[True,'+str(guess)+']')
    else:
        print('[False,'+str(guess)+']')

I want the searchDatabase to search the previously created database. If guess is in the database it will print [True,guess]. It's not searching the created database. How do I make it search the database? I'm assuming I would want to replace "Database" with something else. Thank you.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
coin1002
  • 55
  • 7
  • `return` the database from `createDatabase`, then pass that as an argument to `searchDatabase`. If you provide more of your code (specifically where you are calling these functions) I can give better advice. – Loocid Sep 27 '18 at 00:53
  • Thank you. I don't want the user to be able to see the numbers however, nor do I know how to pass it as an argument. – coin1002 Sep 27 '18 at 00:59

1 Answers1

3

Using classes

One way to do this is implementing those 2 function into one class, like the following:

class Database():
    def __init__(self):
        self.database = []

    def createDatabase(self, size):
        for j in range(0, size):
            # I did'nt get why you pass database here, but I leaved as it is in your code
            self.database.append(randomNumberGenerator(100,self.database))

    def searchDatabase(self, guess):
            # here I'm taking advantage of the test redundancy to shorten the code
            print('[{}, {}]'.format(guess in self.database, guess))

If you get interested to python object oriented programming, see the answer to this question right here in Stack Overflow to get a basic introduction to this subject.

More about python string format used in print here

Example of usage:

db = Database()
db.createDatabase(6)
# Suppose that the database have the following numbers: 4, 8, 15, 16, 23, 42
db.searchDatabase(1)
db.searchDatabase(42)

Output

[False, 1]
[True, 42]

Without classes

def createDatabase(size):
    databse = []
    for j in range(0, size):
        # I did'nt get why you pass database here, but I leaved as it is in your code
        database.append(randomNumberGenerator(100,self.database))
    return database

def searchDatabase(database, guess):
        # here I'm taking advantage of the test redundancy to shorten the code
        print('[{}, {}]'.format(guess in database, guess))

Example of usage equivalent to the "classy" one:

db = createDatabase(6)
# Suppose that the database have the following numbers: 4, 8, 15, 16, 23, 42
searchDatabase(db, 1)
searchDatabase(db, 42)

Gives same output as above

Hemerson Tacon
  • 2,419
  • 1
  • 16
  • 28
  • Thank you. Is there a way to do it without making a class? Your code isn't quite working for me. I get an initialization error. – coin1002 Sep 27 '18 at 02:35
  • @coin1002 check the answer again, please. In relation to the error you mentioned, I edited the post some minutes ago because it really had an error. Try it one more time and let me know if you continues to get the error. – Hemerson Tacon Sep 27 '18 at 02:46
  • Thank you. I did try the class code a second time and it worked then. I know it's unrelated, but how would I use the input size in a separate operation? For instance if outside the function if I want to do size+1? Thanks again. – coin1002 Sep 27 '18 at 03:11
  • You want to update de database? If yes, you would need a function that append new random values to the existig database, which could be passed as a parameter or accessed through `self` in the class version – Hemerson Tacon Sep 27 '18 at 03:15
  • No, I don't want to update the database. I just want the user to be able to use what they input already separately without having to type their input in again. – coin1002 Sep 27 '18 at 03:20
  • I don't know if I understood, but by your description it seems that you want to store the input into a variable, and them use it later. Maybe will be better if you ask another question. By the way, if my answer really helped you, a answer acceptance would be appreciated ;) – Hemerson Tacon Sep 27 '18 at 12:38