Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python: create random values with length controlled via input()

I want to generate a set of random characteres with a lenght determined with the value to pass via input()

import random
import string
a = []  # an empty array created
#length = int(input("Number of character in Password"))
length = input("Number of character in Password")
for _ in range(length):
    a.append(random.choice(string.hexdigits))
b = ''.join(map(str, a))  # to remove gaps and joining elements of array a
print("The Generated Password is " + b)

#below error message

File "C:\Users\carlos\Desktop\folder_learn\file                                   tttuntitled-7.py", line 6, in <module>
  for _ in range(length):

builtins.TypeError: 'str' object cannot be interpreted as an integer

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

just use int() to convert what a string to an integer in Python

length = int(input("Number of character in Password"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading