I want to display the last element of a random number.
I want to create something like a life game using random.randint.
But in the following, what method is used for "step"
I don't know how to display "Random numbers from last rolling dice".
There was an instruction such as "-1" for index, but I don't know how to specify it.
↓ (quoted text)
On each iteration, set step equal to the last element in the random_walk list.You can use the index -1 for this.
# Numpy is imported, seed is set # Initialize random_walk random_walk =  # Complete the ___ for x in range (100): # Set step: last element in random_walk step = random_walk [-1] # Roll the dice dice = np.random.randint (1,7) # Determine next step if dice<= 2: step = step-1 elif dice<= 5: step = step + 1 else: step = step + np.random.randint (1,7) # append next_step to random_walk random_walk.append (step) # Print random_walk print (random_walk)
Answer # 1
In Python, you can specify an element from the right side by specifying a negative index.
>>>lst = ['s', 'p', 'a', 'm'] >>>lst [-1] 'm' >>>lst [-2] 'a' >>>lst [-3] 'p' >>>lst [-4] 's'
- python - i want to display images at random
- python - def to display the number of products correctly
- python - to make 6 question items in a set of 3 questions random
- when graphing a list with matplotlib in python, the number of elements is large and the graph is not displayed
- python - count by element with pivot_table in pandas
- python - does the number of arguments match?
- a large number of errors when installing the python library
- i want to set the maximum value of the slider in python to the number entered in the text box
- adding an element to a particular line in a python 2d list
- java - i want to put a random number in an int type array, cast it to a char type array, and repeat it 4 times to output a 4-dig
- php - i want to display the number of posts in the grandchild category in wordpress
- python matplotlibpyplot i get an error trying to display a moving average on a candlestick chart
- how to scrape in python and display in a browser?
- i want to delete a partially matching element in a python array
- python - data measurement by increasing the number of channels
- [javafx] i want to dynamically display data of any number of columns acquired from mysql in tableview
- python - optuna what is the typical number of searches?
- i want to get the maximum even number from the python list
- i want to get the value from a constant in python and display it