0

I started learning python recently and I want to become more knowledgable and flexible with using loops.

My question is, let's say I created a list of names:

names = ('Benjamin', 'Damien', 'Dexter', 'Jack', 'lucas', 'Norman', 'Thorn', 'Bella', 'Blair', 'Ivy', 'Lilth', 'Megan', 'Rue', 'Sabrina', 'Samara', 'Anthea', 'Jessica', 'Igor', 'Luther', 'Boris', 'Abel', ) 

and now I want to use either a for loop or a while loop (I don't know which one is the best, but from my novice experience, I feel a for loop would work better but I can't find a solution) to generate random number and make my loop generate that amount of names.

This was my code but it doesn't work.

 people_count = int(input("Enter how many names you wish to have, min 0, max 20: ")) for l in range(people_count): generate_random_names = random.choice(names) print (generate_random_names) 

and I only get one random name. What is missing/mistake here?

Thanks in advance!

4
  • For the record, that is not a list of names, but a tuple. Also, it's not really a random number, since the user is inputting it ;) Commented Mar 23, 2017 at 19:05
  • Definitely not a list lol :D Commented Mar 23, 2017 at 19:07
  • If you have the same code indentation as in this example, the you will loop through the whole range, every time replace the generate_random_names, and after the code exits the loop you will print the last generate_random_names value Commented Mar 23, 2017 at 19:08
  • If you read the doc for random.choice you can see that it returns a single element. Essentially, what your loop is doing is assigning a random pick from the names tuple and assigning it to a variable again and again. Basically you're just getting the last value generated in the loop. Commented Mar 23, 2017 at 19:08

5 Answers 5

3

In your for loop you are replacing the variable generate_random_names everytime the loops iterate. Instead maybe put them into a list. Note choice() will give you duplicate values if that's what you wish for.

people_count = int(input("Enter how many names you wish to have, min 0, max 20: ")) generate_random_names = [] for l in range(people_count): generate_random_names.append(random.choice(names)) print (generate_random_names) 

Now you will have a list of names

Also you don't need a for loop to do what you want, you could use a list comprehension:

people_count = int(input("Enter how many names you wish to have, min 0, max 20: ")) generate_random_names = [random.choice(names) for _ in range(people_count)] 

Heck if you don't want to have duplicate values in new list, you can use sample which takes a collection to choose from and how many you want in the new list.

people_count = int(input("Enter how many names you wish to have, min 0, max 20: ")) generate_random_names = random.sample(names,people_count) print(generate_random_names) 
Sign up to request clarification or add additional context in comments.

2 Comments

Ingenious. Thank you!
@JohnnyKang feel free to accept one of the many answers here if answered your question. Just click the check mark.
1

you are looking for random.sample:

from random import sample names = ('Benjamin', 'Damien', 'Dexter', 'Jack', 'lucas', 'Norman', 'Thorn', 'Bella', 'Blair', 'Ivy', 'Lilth', 'Megan', 'Rue', 'Sabrina', 'Samara', 'Anthea', 'Jessica', 'Igor', 'Luther', 'Boris', 'Abel') print(sample(names, k=4)) # ['lucas', 'Ivy', 'Dexter', 'Abel'] 

where the k parameter is the number of elements you want so select.

random.choice will always select one single element only.

Comments

0

You are assigning always to the same variable so every time you iterate you overwrite the same variable and the result is the string from the last iteration.

To acheive what you want you have to add the random name string to some kind of list or array inside the for loop

Comments

0

You are assigning the value to variable in loop "n" times and so when you print, the last assigned value is returned.

What you need to do is store those random names something like this :

import random names = ('Benjamin', 'Damien', 'Dexter', 'Jack', 'lucas', 'Norman', 'Thorn', 'Bella', 'Blair', 'Ivy', 'Lilth', 'Megan', 'Rue', 'Sabrina', 'Samara', 'Anthea', 'Jessica', 'Igor', 'Luther', 'Boris', 'Abel', ) people_count = int(input("Enter how many names you wish to have, min 0, max 20: ")) generate_random_names_list = [] for l in range(people_count): generate_random_names_list.append(random.choice(names)) print(generate_random_names_list) 

For input as 3, this resulted in something like this :

['Norman', 'Rue', 'Rue'] 

To get only the unique names inside the list, you can do something like this :

import random names = ('Benjamin', 'Damien', 'Dexter', 'Jack', 'lucas', 'Norman', 'Thorn', 'Bella', 'Blair', 'Ivy', 'Lilth', 'Megan', 'Rue', 'Sabrina', 'Samara', 'Anthea', 'Jessica', 'Igor', 'Luther', 'Boris', 'Abel', ) people_count = int(input("Enter how many names you wish to have, min 0, max 20: ")) generate_random_names_list = [] while len(generate_random_names_list) < people_count : choice = random.choice(names) if choice not in generate_random_names_list : generate_random_names_list.append(choice) print(generate_random_names_list) 

This will result in something like this for 10 as input :

['Boris', 'Lilth', 'Jack', 'Igor', 'Luther', 'lucas', 'Damien', 'Rue', 'Abel', 'Blair'] 

1 Comment

Note that this can insert in the resulting list repeated names. To get unique names we would need to copy names (if we don't want to lose its original value) and remove the choice from it once inserted in the resulting list.
0

I would use random lib then I would do dandom.randint(x,y) This way you wold return a random number from x to y. Then you could access your name list using

name_list[random.randint(0,len(name_list)] right, you have a random name, now you need to save the value in another list

by doing variable=name_list[random... you can write the name in the var, but everytime you do it you wolud be overwriting so... v=[] v.append(name_list[random.randint(0,len(name_list)]

right? any doubts feel free to ask

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.