July 27th, 2013, 10:59 PM

Generating integers with functions
I want to define a function A that creates a list of N random integers where each integer is a random number between 1 and 10 and N is given to the function as an argument. Then define another function to output A. Define a function that takes A as argument and returns the average of all elements, as well as the maximum deviation
Why doesn't this work so far?
Code:
import random
def N():
N = int(raw_input("Enter a value for N:"))
return N
N()
def A(N):
list = []
for i in range(N):
list.append(random.randrange(1,11))
return list
print list
July 27th, 2013, 11:34 PM

Variables defined within functions are local to those functions. You need to use the return values. Also, list is a builtin. Don't use it as a variable name.
python Code:
import random
def how_many():
N = int(raw_input("Enter a value for N:"))
return N
def make_range(N):
mylist = []
for i in range(N):
mylist.append(random.randrange(1,11))
return mylist
def main():
N = how_many()
print(make_range(N))
main()
Mek
Comments on this post
July 27th, 2013, 11:50 PM

thank you!! is it possible to output mylist if i wanted to use it as an argument for main()
something similar to this?
Code:
def main(mylist):
N=how_many()
print (make_range(N))
main(make_range(N))