-1

I tried making a command where it picked 10 random people from a list of 20, and listed it from top to bottom, but I realized straight after that it sometimes repeats peoples names. Is there a way to fix that? Here is my code.

@client.command(name='people')
async def people(ctx):
  peopleNames = [
    "person1",
    "person2",
    "person3",
    "person4",
    "person5",
    "person6",
    "person7",
    "person8",
    "person9",
    "person10",
    "person11",
    "person12",
    "person13",
    "person14",
    "person15", 
    "person16",
    "person17",
    "person18",
    "person19",
    "person20",
  ]

  await ctx.send(f'**LIST OF TOP 10 PEOPLE**\n\n{random.choice(peopleNames)}\n{random.choice(peopleNames)}\n{random.choice(peopleNames)}\n{random.choice(peopleNames)}\n{random.choice(peopleNames)}\n{random.choice(peopleNames)}\n{random.choice(peopleNames)}\n{random.choice(peopleNames)}\n{random.choice(peopleNames)}\n{random.choice(peopleNames)}')
DLG
  • 3
  • 2

1 Answers1

1

Issue:

Using random.sample we can get exactly what you need.

@client.command()
async def people(ctx):
    people_names= [f'person{i}' for i in range(20)]
    names_formatted = ', '.join(random.sample(people_names, k=10))
    await ctx.send(f'List of top 10 people: {names_formatted }')

Keep in mind:

I would like you to keep in mind discord.py is not a beginner friendly library. It is expected that you know the basics of Python before using it. Feel free to check out these resources to help you if you need:

Resources:

https://docs.python.org/3/tutorial/ (official tutorial)

http://python.swaroopch.com/ (useful book)

https://automatetheboringstuff.com/ (for complete beginners to programming)

http://greenteapress.com/wp/think-python-2e/ (another decent book)

See also: http://www.codeabbey.com/ (exercises for beginners)

https://realpython.com/ (good articles on specific topics)

https://learnxinyminutes.com/docs/python3/ (cheatsheet)

Iced Chai
  • 472
  • 2
  • 13