dimanche 5 mai 2019

How can i generate a random names for lists python?

I am making an algorithm to work out the probability of three dice winning against two dice. There are other rules to this but it would take too long to explain.

I am storing the dice scores in a list, but as my program is in a for loop the list names need to be changed each time. Another problem is that so that i don't get the same combinations multiple times, the program is supposed to check that the current list is not the same as previous lists.

I'm running this program on spyder - I have all the modules you get when you install the 64 bit windows version of python 3.

My code is as follows:

import random
i = 0

while i < 10: #Currently the system is repeating a set number of times but it should be until no more lists can be produced 

    defender_score = 0
    attacker_score = 0

    defender_die_1 = random.randint(1,6)
    defender_die_2 = random.randint(1,6)
    attacker_die_1 = random.randint(1,6)
    attacker_die_2 = random.randint(1,6)
    attacker_die_3 = random.randint(1,6)

    list = [defender_die_1, defender_die_2, attacker_die_1, attacker_die_2, attacker_die_3]  #The list that needs to be assigned a random name


#The if statement that checks that the list is unique before excecuting all the code below

    if defender_die_1 >= attacker_die_1:
        defender_score = defender_score + 1

    if attacker_die_1 > defender_die_1:
        attacker_score = attacker_score + 1

    if defender_die_2 >= attacker_die_2:
        defender_score = defender_score + 1

    if attacker_die_2 > defender_die_2:
        attacker_score = attacker_score + 1

    print(list)    
    print(attacker_score, defender_score)

    i = i + 1




Aucun commentaire:

Enregistrer un commentaire