How do I generate a random 4 digit number and store it as a variable in python?

How do I generate a random 4 digit number and store it as a variable in python?

You have a typo. Its random.randint().

import random
number = random.randint(1000,9999)
print(number)

How do I generate a random 4 digit number and store it as a variable in python?

try this:

import string
from random import choice
chars = string.digits
random =  .join(choice(chars) for _ in range(4))

this will make a different 4 digit number every time you run it. if you want to make more digits just change the range.

Leave a Reply

Your email address will not be published. Required fields are marked *