``````import random

availableCountries = [0, 1, 2, 3, 4, 5, 6, 7]
firstTurnsCountries = []
secondTurnsCountries = []

def countryDivider(countryNum):
counter = 0
while True:
if counter == 8:
break
num = 7
if num > 0:
if num == 0:
if counter %2 != 0:
if counter %2 == 0:
counter += 1
num -= 1

countries = len(availableCountries)
countryDivider(countries)
print secondTurnsCountries
print firstTurnsCountries``````

But this code can give the two lists the same numbers. And most of the time it gives each list multiple occurances of a number.

I was trying to add a line that deleted any number that was selected, so that it wouldn't be chosen again, so that's why i made the random number range get smaller with a decreasing counter, because the list would get smaller. But I can't get it to work.

BASICALLY: I need this to divide the numbers in the first list among the other two lists evenly, randomly, and so that no list gets the same country as another list, and no duplicate countries in one list.

I hope this is what you had in mind ...

``````# goal: make two lists of 4 countries from the initial 8 country list
# no list gets the same country as another list, and no duplicate countries in one list

import random

availableCountries = [0, 1, 2, 3, 4, 5, 6, 7]
firstTurnsCountries = []
secondTurnsCountries = []

def countryDivider(countryNum):
while True:
# check if list is spent/empty
if not availableCountries:
break
# pick one available country
q = random.choice(availableCountries)
# add it to first list
firstTurnsCountries.append(q)
# remove it from the available list
availableCountries.remove(q)

# repeat for second list
q = random.choice(availableCountries)
secondTurnsCountries.append(q)
availableCountries.remove(q)

#print availableCountries  # test

countries = len(availableCountries)
countryDivider(countries)
print secondTurnsCountries
print firstTurnsCountries``````

There are most likely better solutions, but I wanted to follow your original code as closely a possible.

If I'm understanding correctly, you want to divy up the countries evenly, but randomly, between the firstTurnsCountries and secondTurnsCountries.

The key, I think, is to remove the countries from the list as you choose them. Thus:

``````availableCountries = range(8)  # [0,1,2,3,4,5,6,7]
fiirstTurnsCountries = []
secondTurnsCountries = []

tmp = availableCountries[:]  # in case you want to keep availableCountries intact

while tmp:
c = random.choice(tmp)
firstTurnsCountries.append(c)
tmp.remove(c)

c = random.choice(tmp)
secondTurnsCountries.append(c)
tmp.remove(c)``````

This code would fail if len(availableCountries) is odd. So:

``````...

player = firstTurnsCountries
while tmp:

c = random.choice(tmp)
player.append(c)
tmp.remove(c)

if player == firstTurnsCountries:
player = secondTurnsCountries
else:
player = firstTurnsCountries``````

which will divy up any list of countries and can be cleanly extended to more than two players.

Jeff

Thanks Guys! :cheesy:

This worked beautifuly. I sortof combined your two responses in the one that I will use in my Risk Game. I am making it so you can choose your own countries or divide them up randomly, this is the latter.

Thanks again,

Matt

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.