Another function I have deciphered is random.randint().
First, you have to type:
import random
Now, what does randint mean? It actually means random integer. Now, type this:
print(random.randint(1, 12))
The output should be a random number. Keep typing the same thing, and you'll notice that the number keeps changing.
You can also use random.randint with lists! Type this:
list=["Hi", "Bye", "Die"]
listInt=random.randint(0, 2)
print(list[listInt])
You should get one of the items from the list. Remember, lists start with 0, not 1. You tell Python to print list, but then listInt in the square brackets tells it to take a random item from the list and print it. What if we did one that's out of range?
listInt=random.randint(0, 15)
print(list[listInt])
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
print(list[listInt])
IndexError: list index out of range
So, it has to be the limit of items in the list.
That's all I know about random.randint, so... That's it.
No comments:
Post a Comment