2021-12-11

Tech Programing

程式人小天地

How to get random names from a list without repeating names. Python

1 min read


I am very new to Python and I have run into a problem that I can’t find the solution for. I am trying to create a unique character generator, to create the names I have a pool of names that are in a .txt file and also suffix’ which are in another one. However, I run into the problem that when I run the code sometimes the same names appear as they are chosen at random each time round. This is the code that I have written for it:

import random

a_file = open("Alien_Names.txt", "r")
a_names = [(line.strip()).split() for line in a_file]
a_file.close()

b_file = open("suffix.txt", "r")
b_names = [(line.strip()).split() for line in b_file]
b_file.close()

def repeat():
fname = random.choice(a_names)
sname = random.choice(b_names)
print(fname, sname)
for i in range (10):
repeat()

I will be thankful for any help.



Source link

資料來源:Stackoverflow

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *