How to create array of the numbers in the loop in python -


i created while loop that's random each time run , need save values of x , y arrays. don't know how because values different each time run code. have far

import numpy np x = 5                                  y = 5                                while true:     = np.random.rand(1)                   print x     if < .5:           x = x + 1                               y = y - 1                      print x         print y     if > .5:                                   y = y + 1         x = x - 1                       print x         print y     if x == 0:                                 print x         break                                if y == 0:                                 print y           break    

you can create empty list each 1 , append new values go.

for example:

import numpy np x = 5    xs = []                               y = 5    ys = []                             while true:     = np.random.rand(1)                   print x     if < .5:           x = x + 1                               y = y - 1                      print x, y     if > .5:                                   y = y + 1         x = x - 1                       print x, y      xs.append(x)     ys.append(y)      if x == 0:                                 print 'y won.'         break                                if y == 0:                                 print 'x won.'         break     print 'xs', xs print 'ys', ys 

sample output:

6 4 7 3 6 4 5 5 4 6 5 5 6 4 5 5 4 6 3 7 4 6 3 7 2 8 1 9 0 10 y won. xs [6, 7, 6, 5, 4, 5, 6, 5, 4, 3, 4, 3, 2, 1, 0] ys [4, 3, 4, 5, 6, 5, 4, 5, 6, 7, 6, 7, 8, 9, 10] 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -