Reputation: 11
I have issue with a code for generate password (code below)
from random import randint, choice
alphabet_min = [chr(i) for i in range(97, 123)]
alphabet_maj = [chr(i) for i in range(65, 91)]
chiffre = [chr(i) for i in range(48, 58)]
carac_spec = [chr(i) for i in range(33, 48)]
def pwd(n, min1=True, maj=True, chif= True, cs=True):
alphabets = dict()
key = 0
if min1:
alphabets[key] = alphabet_min
key += 1
if maj:
alphabets[key] = alphabet_maj
key += 1
if chif:
alphabets[key] = chiffre
key += 1
if cs:
alphabets[key] = carac_spec
key += 1
mdp = ' '
for i in range(n):
clef = randint(0, key - 1)
mdp += choice(alphabets[clef])
return mdp
When I execute this, nothing appear
Code finding on internet and very beginner in Python code.
Anyone have an idea of the issue ?
Upvotes: 1
Views: 76
Reputation: 11
Finaly code working with adding this at the beginning
n = int(input('\nEntrer la longueur du Mdp '))
and code print(pwd(n))
at the bottom
Thanks for the help
Upvotes: 0
Reputation: 83
First of all, you didn't call the function! To call a function, type the name of the function followed by parameters. For example:
def add(a, b):
return a + b
add(1, 2)
But there still is a problem. If you are running this in the IDE, Google Colab, or a Jupyter notebook, you probably would see 3
in the console. However, if you are running this in replit.com, mu, or a python IDE file, nothing appears. Now, you need to call print()
. The solution to your problem is:
from random import randint, choice
alphabet_min = [chr(i) for i in range(97, 123)]
alphabet_maj = [chr(i) for i in range(65, 91)]
chiffre = [chr(i) for i in range(48, 58)]
carac_spec = [chr(i) for i in range(33, 48)]
def pwd(n, min1=True, maj=True, chif= True, cs=True):
alphabets = dict()
key = 0
if min1:
alphabets[key] = alphabet_min
key += 1
if maj:
alphabets[key] = alphabet_maj
key += 1
if chif:
alphabets[key] = chiffre
key += 1
if cs:
alphabets[key] = carac_spec
key += 1
mdp = ' '
for i in range(n):
clef = randint(0, key - 1)
mdp += choice(alphabets[clef])
return mdp
print(pwd(5))
Upvotes: 1
Reputation: 171
You are not printing anything. You display stuff in console by using print() statements For example
print("hello")
shows
hello
In this case you want to "print" the return value of pwd function
print(pwd(10))
The 10 is the first argument "n" so in this case the length of a generated password, by changing this number you will get different lengths.
Generally StackOverflow is not a tutorial site, please try to learn basics of language on your own, then try to google problems, and only then if google won't help, post questions here
Upvotes: 1