What can I do to prevent using same roll for two stats?
Append the random rolls to a list and use each value in the list once only. If I misunderstand and this is not what you want, then post back.
import random
## random can come up with the same number twice, so append
## integers until 6 unique numbers are in the list
def random_rolls():
rolls_list = []
ctr = 0
while ctr < 6:
x = random.randint(3,18)
if x not in rolls_list:
rolls_list.append(x)
ctr += 1
return rolls_list
#this will assign rollOne for strength stat
def chooseStrength(rolls_list):
strengthChosen = raw_input("Choose a roll number between 1 and %d: " \
% (len(rolls_list)))
## arrays are accessed via memory offset, so use
## zero through 5 instead of 1 --> 6 (one less)
strengthChosen_int = int(strengthChosen) - 1
strength = rolls_list[strengthChosen_int]
print 'You have chosen the %s roll as your strength.Your strength is %d.' \
% (strengthChosen, strength)
## now delete that number so it's not used again
del rolls_list[strengthChosen_int]
return strength, rolls_list
##-------------------------------------------------------------------
rolls_list = random_rolls()
print "You would use these numbers for the rolls:"
for x in rolls_list:
print x
print "\nLet's do it again to test for different numbers"
rolls_list = random_rolls()
print "You would use these numbers for the rolls:"
for x in rolls_list:
print x
## copy the list if you want to keep the original
orig_rolls = rolls_list[:]
## now chooseStrength as an example of how to use it
strength, rolls_list = chooseStrength(rolls_list)
print "Strength has been set to", strength
print "\nand rolls_list is now smaller"
for x in rolls_list:
print x
And it might be easier to assign each roll to a dictionary key pointing to "strength", etc. or vice-versa. Also, within a class, you can define the list or dictionary as
self.rolls_list = []
and it will be visible throughout the class, i.e. you don't have to pass and return to/from functions.
Last edited by woooee; Jun 2nd, 2009 at 9:31 pm.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
Offline 2,305 posts
since Dec 2006