Hello everyone I learned the basics of python. I wanted to start a project on a magic8ball. It didn't worked so I searched a bit and found a working code. He uses this weird thing and I don't know why. Can someone explain what this means?
choice()
returns a random item from a list, tuple, or string while randint()
returns a pseudorandom integer between a and b. You can use either, assuming you are passing it the right parameters. Say you have
a = [1, 2, 3, 4, 5, 6, 7]
import random
random.choice(a) # will return a random number from that list
random.randint(1,7) # will return a random number between 1 and 7 itself
In your case,
answers[randint(0, len(answers) - 1)]
will give you a random integer between 0 and however long the list of answers is minus one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.