0

Hi
I have a function which rand digit between a and b. it maybe 2,3 or more digits. I want to save these digits in a list and then use them in another part of my program. How can I do it?

def dela(x):
    sum=0
    while sum <x:
        b= random.randrange (1,x-sum+1)
        sum = sum+b
        print b,
    return sum
2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Mackjan
0

Something like this might do it:

import random

def dela(x):
    sum=0
    mylist = []
    while sum < x:
        b = random.randrange (1, x - sum + 1)
        mylist.append(b)
        sum = sum + b
        #print b,
    return sum, mylist

# test it ...
sum, mylist = dela(5)
print mylist  # eg. [2, 1, 1, 1]
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.