Reputation: 155
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!
Upvotes: 0
Views: 1241
Reputation: 4991
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)
Upvotes: 3
Reputation: 99
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
Upvotes: 0
Reputation: 2233
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']
Upvotes: 0
Reputation: 46921
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.
Upvotes: 1
Reputation: 848
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
Upvotes: 0