jeudi 6 août 2020

Trying to define a function that creates lists from files and uses random.choices to choose an element from the weighted lists

I'm trying to define a function that will create lists from multiple text files and print a random element from one of the weighted lists. I've managed to get the function to work with random.choice for a single list.

enter code here

def test_rollitems():
    my_commons = open('common.txt')
    all_common_lines = my_commons.readlines()
    common = []
    for i in all_common_lines:
        common.append(i)
    y = random.choice(common)
    print(y)

When I tried adding a second list to the function it wouldn't work and my program just closes when the function is called.

enter code here
def Improved_rollitem():
    #create the lists from the files#
    my_commons = open('common.txt')
    all_common_lines= my_commons.readlines()
    common = []
    for i in all_common_lines:
        common.append(i)
    my_uncommons = open('uncommon.txt')
    all_uncommon_lines =my_uncommons.readlines()
    uncommon =[]
    for i in all_uncommon_lines:
        uncommon.apend(i)

    y = random.choices([common,uncommon], [80,20])
    print(y)

Can anyone offer any insight into what I'm doing wrong or missing ?




Aucun commentaire:

Enregistrer un commentaire