0

Python name generation with out repeating

(Long Question) If I am making a game that has tasks for one sometimes two players. How would i randomly generate the second players name without running the risk of repeating the same name? (Short Question) How do i randomly generate 2 names with out running the risk of repeating?

2nd Oct 2020, 2:43 PM
Tom Klein
Tom Klein - avatar
5 Respuestas
+ 6
the principle is rather simple: - create a list with n names - select ramdomly the first name - select randomly the second name and compare it to the first: - if the second name is identical with the first name, repeat the selection of the second name
2nd Oct 2020, 3:14 PM
Lothar
Lothar - avatar
+ 6
Tom Klein , it would be great if you could post us your current try in coding for this task. this makes us able to help you. Thanks!
2nd Oct 2020, 3:38 PM
Lothar
Lothar - avatar
+ 5
Tom Klein , see my comments for you in the attached file. https://code.sololearn.com/c3TFbrFR4lRN/?ref=app
3rd Oct 2020, 7:44 PM
Lothar
Lothar - avatar
+ 1
What would the code be to compare? Would it be: if x == x run again ?
2nd Oct 2020, 3:19 PM
Tom Klein
Tom Klein - avatar
+ 1
def select_player(): import random players= ['Tom', 'Crystal', 'Joe', 'James', 'Morgan'] print (random.choice(players)) #print(value) print('\n') select_player() # E=000-299/a-zz # M=300-699/aaa-azz # X=700-999/baa-bzz def select_card(): rating = input() if (rating == "e"): import random lis = [] lis += list(range(17, 18)) value=(random.choice(lis)) if (rating == "m"): import random lis = [] lis += list(range(1, 45)) lis += list(range(300, 331)) value = (random.choice(lis)) #print(value) if (rating == "x"): import random lis = [] lis += list(range(1, 45)) lis += list(range(300, 331)) lis += list(range(700, 731)) value = (random.choice(lis)) print(value) #print (value) print ('\n') def two_player(): cards = (17, 39, 40, 45, 300, 302, 308, 315, 319, 323, 330, 703, 704, 705, 718, 719, 720, 724, 727) for number in cards: if number==value: select_player() print ('***If the same name gets picked twice***\n***the rest of the group gets to decide***\n ***your parnter***') print ('\n') two_player()
2nd Oct 2020, 3:44 PM
Tom Klein
Tom Klein - avatar