View Single Post
Old May 3rd, 2008, 9:37 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,836
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Are nested lists indestructable?

It's not that it's not working. What appears to be a lack of randomness is actually a side-effect of not creating new variables.

When you create variables inside a function, they need to be returned back to where the function was called if you want to access them.

from random import randint

namelist=['Fred', 'Joe', 'Edward', 'Eric', 'John', 'Ryan', 'Chuck', 'Henry', 'Jean', 'Robert', 'Guido', 'Alan', 'Nicolas', 'Bill']

def whrstat():
  dice=randint(0, 13)
  whrname=namelist[dice]
  att=randint(1, 5)
  deff=randint(1, 5)
  return [whrname, att, deff, 1]

aa = whrstat()
print aa

ab = whrstat()
print ab

['John', 4, 2, 1]
['Henry', 1, 2, 1]

What that will do is create the variables inside the function, return them as a list, and then assign them to whatever's calling the function.

If the explanation for this behaviour does not seem obvious to you, I suggest that you do a little review on how to use functions and return.
Sane is online now   Reply With Quote