How do i make this code to store data in a global variable, it create random number of elements.

``````n = random.randint(0, 99)
for i in range(1):
print "-"*60
print "Here is some new elements! "

while n >= 0:
data_list = random.randint(0,99)
print data_list,
time.sleep(.10)
n= n-1``````

## Recommended Answers

You need to start with an empty list and append to it ...

``````import time
import random

data_list = []
n = 10
while n >= 0:
data_list.append(random.randint(0, 99))
print data_list
time.sleep(0.1)
n = n - 1``````

There are a couple things to set right with your code first.

``````n = random.randint(0, 99)
for i in range(1):
``````

First, don't indent the `for i in ....` part. It is a code block itself.

Second, why have a code block that loops once. Just …

A try/except error handling will get you there. For example ...

``````import random

# create a list of 10 random integers from 0 to 99
# using a list comprehension
randint_list = [random.randint(0, 99) for k in range(10)]

print randint_list
print '-'*40

# test try/except and pop() …``````

## All 11 Replies

You need to start with an empty list and append to it ...

``````import time
import random

data_list = []
n = 10
while n >= 0:
data_list.append(random.randint(0, 99))
print data_list
time.sleep(0.1)
n = n - 1``````

There are a couple things to set right with your code first.

``````n = random.randint(0, 99)
for i in range(1):
``````

First, don't indent the `for i in ....` part. It is a code block itself.

Second, why have a code block that loops once. Just drop it.

The only loop you have left then is the while loop, now, not being in the for loop anymore (which it would have been), the while can also go right back to the line. The only indented stuff will be what follows it.

Okay, now to your question

How do i make this code to store data in a global variable, it create random number of elements.

Well, everything within this script is within the global namespace, so no worries there.

All you need to do is to save it in a list. In your example, your data_list is actually not a list at all, it's an int. Why not change that to data_element, and use data_list for the whole list.

``````import random, time

n = random.randint(0, 99)
data_list = []

print "-"*60
print "Here is some new elements! "

while n >= 0:
data_elements = random.randint(0,99)
print data_elements,
time.sleep(.10)
data_list.append(data_elements)
n= n-1

print "\ndata_list has %d elements" % len(data_list)
``````

Thanks for the help..it clear some stuff

Thanks for the help..it clear some stuff.

It works rather well to create the elements,
and if i want to remove random amounts of elements from the data_list?

If the random amounts of "remove" elements is bigger then the amount of elements already in the data_list it should not try to remove elements from it.. i mean that if there isnt enough elements to delete in data_list that process should wait until there is enough elements.

A try/except error handling will get you there. For example ...

``````import random

# create a list of 10 random integers from 0 to 99
# using a list comprehension
randint_list = [random.randint(0, 99) for k in range(10)]

print randint_list
print '-'*40

# test try/except and pop()
# deliberately exceeding the index range
for k in range(12):
try:
# pop an element off the list
# by default it is the last element of the list
element = randint_list.pop()
print element, randint_list
except:
# break out of the loop on error (most likely an index error)
break

"""my possible result -->
[34, 76, 61, 41, 23, 85, 91, 55, 30, 58]
----------------------------------------
58 [34, 76, 61, 41, 23, 85, 91, 55, 30]
30 [34, 76, 61, 41, 23, 85, 91, 55]
55 [34, 76, 61, 41, 23, 85, 91]
91 [34, 76, 61, 41, 23, 85]
85 [34, 76, 61, 41, 23]
23 [34, 76, 61, 41]
41 [34, 76, 61]
61 [34, 76]
76 
34 []
"""``````

i mean that if there isnt enough elements to delete in data_list that process should wait until there is enough elements

Can you just compare the length of data_list to the number of random elements you wish to delete?

Ehh ok i will try to figure that out..but i still dont know how i remove a element from for example data_list?

if i want to remove random amounts of elements from the data_list?
how do i do that?

Can you just compare the length of data_list to the number of random elements you wish to delete?

I tried that but it didnt work out..

I tried that but it didnt work out..

Post the code that you are using.

I tried that but it didnt work out..

Show us the code you used.

``````import time
import random
from threading import Thread
from threading import Lock

data_list = []

def main():

global lock
lock = Lock()

thread0 = P()
thread0.start()

thread1 = C()
thread1.start()

class P(Thread):

def run(self):

lock.acquire()
for i in range(5):
print "Thread: Yo! from thread 0 " , i
time.sleep(1)
lock.release()

n = random.randint(0, 99)

print "Here is some new elements! "

while n >= 0:
data_elements = random.randint(0, 99)
print data_elements,
time.sleep(.10)
data_list.append(data_elements)
n = n-1

print "-"*60
print "\ndata_list has %d elements" % len(data_list)

class C(Thread):

def run(self):

lock.acquire()
for i in range(5):
print "Thread: Yo! from thread 0 " , i
time.sleep(1)
lock.release()

m = random.randint(0, 99)

print "Here is will remove some elements! "

while m >= 0:
data_elements = random.randint(0, 99)
print data_elements,

data_list.remove[data_elements]
time.sleep(.20)
m = m-1

print "-"*60
print "\ndata_list has %d elements" % len(data_list)

if __name__ == '__main__':
main()``````

How do i make the C class remove data from data_list and it must do a check before it try to remove or it should not start or wait until enough element . The problem is that it try to consume a element that dont exist in the data_list. and often more elements as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.