vendredi 22 septembre 2017

how to replace double list items which got appended but random module?

So i have some code that makes an user append words in a list. And lets say the user appended 5 words. lst = ['The', 'first', 'letter', 'word' 'yes'] and we need to print 3 random words from the list, but the may not be the same. So when I use the random module and it prints 2 of the same words it replaces one of them, and if it prints the same word again it replaces him immidiatly. this is my code hope you understand it. the code needs to be where the * are.

  • import random

    print('You can choose from randnormal and normextra.') print('Randnormal is a one time thing, so it can be that some people are showed twice,') print("but at randspecial people can't be chosen twice, so choose one") choose = input("randspecial(S) or randnormal(N): ") breaksystem = '1' while True: def again(): print() again = input('Do you want to do this again (Y/N): ') if again.lower() == 'y': print() elif again.lower() == 'n': breaksystem = '0' else: print('Try again:') print() again()

    def randnormal():
        ppl = int(input('From how many people do you want to choose: '))
        print()
        lst = []
        for i in range(0, ppl):
            inp = input('Name of the person: ')
            lst.append(inp)
    
        many = int(input('How many people do you want to select: '))
        if many == 1:
            print(random.choice(lst))
            again()
        elif ppl < many:
            print('You want to select more people than you have chosen.\n')
            again()
        else:
            for i in range(0, many):
                rand = random.randint(0, len(lst) - 1)
                print(lst[rand])
            again()
    
    def randspecial():
        ppl_ = int(input('From how many people do you want to choose: '))
        print()
        lst_ = []
        for i in range(0, ppl_):
            rand_ = input('Name of the person: ')
            lst_.append(rand_)
        many_ = int(input('How many people do you want to select: '))
        if many_ == 1:
            print(random.choice(lst_))
        elif ppl_ < many_:
            print('You want to select more people than you have chosen.\n')
            again()
        else:
            THE CODE SHOULD BE HERE *
    
                elif i == len(new_list- 1):
                    print(new_list)
                    print('It tried {} time before his decision was made.'.format(total))
                    break
                else:
                    total += 1
    
    
    
    
    if choose.lower() == 's':
        randspecial()
    elif choose.lower() == 'n':
        randnormal()
    else:
        print('Sorry pick again.')
        print()
    
    



Aucun commentaire:

Enregistrer un commentaire