Discord
Login
Community
DARK THEME

List

Hello, I have a question. If you make a list, how do pull random things out of it, and/or random amount out of it? Thanks! :)

For example

list =  [
"fruit/apple"
"fruit/orange"
]
 or something like:

list = [
1
2
3
4
]

How to call those into a variable or draw it on screen or something?

currently that's like a object what you need to do is list = [1,2,3,4,5] and to call an item find its position in the list minus 1 so the position of 1 is 0 all lists start at 0 so list[0] == 1 list[1] == 2

so would is do list.push(1) or something? what I mean is how to I represent it?

Hey P1X3L_2012,

To pick a random item out of your list, you can use the built in random functions. microStudio lists are 0 indexed, meaning the first item is at position 0, the second is at 1, and so on.

Here is a quick example of how you can grab a single random item and save it to a variable:

my_list = ["fruit/apple", "fruit/orange", "fruit/banana"]

random_item = random.choice(my_list)

If you want to draw that random item directly to the screen inside your draw loop, you can do this:

screen.drawText(random_item, 0, 0, 20, "#FFF")

If you want to grab multiple random items from the list, you can shuffle the list first and then use a loop to pull out as many as you need! :)

Thank you, TiberiuesCreator2 and Ajiscool. This really helped me! :D

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community