Arik
Arik

Reputation: 71

Returning of random values

I've created random person generator. Divided into functions and class that define the person. I'm using a Json file for the names. But when I run the code, the name of the person, and the email are diffrent random names, although they are on the same function. I'm guessing it as something to do with wrong return of the function.

def Person():
    files = ['global', 'local']
    file = choice(files)
    with open('names/{}.json'.format(file)) as f:
        data = json.load(f)
    randomFirst = randint(0, (len(data['first']) - 1))
    randomLast = randint(0, (len(data['last']) - 1))
    firstName = data['first'][randomFirst]
    lastName = data['last'][randomLast]
    email = firstName.lower() + '.' + lastName.lower() + str(suffix) + choice(domain)

    return firstName, lastName, email

user = User(Person()[0], Person()[1], Person()[2])

Output:

Name:Sarah Gagnon, Email:[email protected]

Upvotes: 0

Views: 39

Answers (1)

Sayse
Sayse

Reputation: 43330

You're calling the function multiple times, denoted by (), instead you can either just unpack the values that are returned or assign it to a local variable

user = User(*Person())
user = User((p:= Person())[0], p[1], p[2])
p = Person()
user = User(p[0], p[1], p[2])

Upvotes: 1

Related Questions