How do I generate a random 4 digit number and store it as a variable in python? [closed]
Emily Wong
I'm trying to make a guess the number game in Python, but I'm having trouble. Here is what I have so far:
import random
number = random.randit(1111,9999)
print(number)But for some reason I get an error saying something about random cannot be assigned to randit.
Any help please?
23 Answers
You have a typo. It's random.randint().
import random
number = random.randint(1000,9999)
print(number) 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.