-3

I am trying to create a random password generator in python .

How do I select like 3 or 4 or 5 random number from a list using user.

For example :

numbers = ['0','1','2','3,','4','5','6','7','8',9']

numbers_in = int(input('how many numbers would you love to be in your password:').

If i choose 4 as a user how will get four random numbers from a list like 4791

  • 1
    Does this answer your question? [Generate password in python](https://stackoverflow.com/questions/3854692/generate-password-in-python) – Benoît Zu Aug 18 '21 at 08:39

1 Answers1

1

Here you go:

>>> from random import choice
>>> from string import digits
>>> N = int(input('How many numbers would you love to be in your password: '))
How many numbers would you love to be in your password: 4
>>> "".join(choice(digits) for _ in range(N))
'1786'

Shorter version of the last statement provided by @furas in comments:

"".join(choices(digits, k=N))
Roman Pavelka
  • 3,736
  • 2
  • 11
  • 28