0

I'm trying to code a program that would pickup randomly a name from a list (which is imported from a .txt file, where there is one name for one line). Then I want to save that name in another file. After one name picked up, the program would ask the user if he wants to pick up another name (another one). Ideally, when all the names are picked up, the program would restart to pick up randomly names from the beginning. For information I'm supposed to compare lists, but I think it's too difficult for me at this moment.

    import random

#Open the files which contains the list of names
#I created a list from that file
file1 = open('Employees.txt', 'r')
listEmployee = file1.readlines()
listEmployee[0]

#open the empty list, to be ready to be filled with the
#futur picked up names
file2 = open('tested.txt', 'r')
listTested = []   

#I created a loop:
#It will pick up a random name, then ask the user if he want to continu or quit
again = 1
while again == 1:
    if again == 1:
        employee = random.choice(listEmployee)
        print ('The employee selected is: ' +(employee)+ '\n')

        #write the name of the employee in the file
        with open('tested.txt', 'a') as file:
            file.writelines(str(employee)+'\n')

        again = int(input('Select another employee: press 1 \nOr quit, press: 2 '))
        print('')

    #if the user press 2 : quit the program
    elif again == 2:
        quit()

    #if the user doesnt press 1 or 2
    elif again != 1 or again != 2:
        again = int(input('Please select 1 or 2: \nSelect another employee: press 1 \nQuit, press: 2 \n'))
        print ('')

It kind of work, but the program might still select twice the same name in a row (which is what I want to avoid).

So for now, when I run the program, it gives me a name and asks me if I want to select another name or quit the program. If I select another name, it will possibly give me the same name, or a name that was already picked up even if all of the name in my list weren't previously picked up (I'm sorry, I'm trying to be as clear as possible).

So I read (and received some advice) about using the shuffle method, but when I try to use that, it returns the whole list randomized, not just one name.

Pak
  • 157
  • 1
  • 1
  • 6
  • Possible duplicate of [How to randomly select an item from a list?](https://stackoverflow.com/questions/306400/how-to-randomly-select-an-item-from-a-list) – underscore_d Dec 08 '17 at 12:53
  • Thank you. I looked at that (several times !). I tried some answers that seemed to be kind of applicable with me. Unfortunately, I'm stuck on this page, there is something that I don't understand yet. I'm sorry if my question is redondant... – Pak Dec 08 '17 at 13:06
  • How about removing the element from the list that was chosen by the random function something like `while employee in listEmployee: listEmployee.remove(employee)`. This would remove it from the list and hence wont be chosen again. – Nikhil Fadnis Dec 08 '17 at 18:47
  • It's a good idea that I can understand, thank you. But when all them names are picked up, is it possible to restart the list from the beginning ? – Pak Dec 08 '17 at 20:43

0 Answers0