#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    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

    • lunapt agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0
    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))

IMN logo majestic logo threadwatch logo seochat tools logo