I'm trying to create a list of names, with each one being different. Here is my code right now, but all it does it create multiple instances of the same name.
import random
first_names=('John','Andy','Joe')
last_names=('Johnson','Smith','Williams')
full_name=random.choice(first_names)+" "+random.choice(last_names)
group=full_name*3
For example, this would show up as:
John Smith
John Smith
John Smith
But I want something like:
John Williams
Andy Johnson
Joe Johnson
Why you complicate such a simple task :)
Maybe this will help: https://faker.readthedocs.io/en/master/index.html
from faker import Faker
fake = Faker()
for i in range(0, 10):
print(fake.name())
Faker has a lot of usefull providers you can use: https://faker.readthedocs.io/en/master/providers.html
you're just duplicating your string here. Random occurs only once.
Do it in a generator comprehension instead, and join the results with space:
import random
first_names=('John','Andy','Joe')
last_names=('Johnson','Smith','Williams')
group=" ".join(random.choice(first_names)+" "+random.choice(last_names) for _ in range(3))
print(group)
outputs:
Joe Williams Joe Johnson Joe Smith
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With