How do I get my random password generator in Python to work?

69 Views Asked by At

I'm in a college class for programming and I have to make a random number generator using Python, but I can never seem to get it to work. I either get an error or nothing gets displayed. Currently the error I'm getting with this version is

line 34, in <module> 
print(rand.randchoice(X) in range(Y))
return seq[self._randbelow(len(seq))]
Type Error: object of type'int' has no len()
import os
import sys
import random as rand   
import string as str

#FUNCTION FOR THE UI
print('Please choose a difficulty level for your new password:')
print('1. Easy')
print('2. Medium')
print('3. Hard')
X = int(len(input('Enter Your Choice Here: ')))
Y = int(len(input('Enter Number of Characters Here: ')))

easy = str.ascii_lowercase
medium = str.ascii_lowercase + str.ascii_uppercase + str.digits
hard = str.ascii_letters + str.ascii_uppercase + str.digits + str.punctuation

if X == '1':
    X = easy
elif X =='2':
    X = medium
elif X == '3':
    X = hard

print(rand.choice(X) in range(Y))

I also tried putting in all of the characters in to the arrays manually. I was hoping for it to get random characters from each list with a length that matches "Y".

The first line of code is line 7, the last line is line 34

2

There are 2 best solutions below

1
ncica On BEST ANSWER

The error is occurring because the variable y is an integer representing the length of the input string, rather than the number of characters the user wants in their random string.

import random as rand
import string as string_module

print('Please choose a difficulty level for your new password:')
print('1. Easy')
print('2. Medium')
print('3. Hard')

x_choice = input('Enter Your Choice Here: ')
y_chars = int(input('Enter Number of Characters Here: '))

easy = string_module.ascii_lowercase
medium = string_module.ascii_lowercase + string_module.ascii_uppercase
hard = string_module.ascii_letters + string_module.digits + string_module.punctuation

if x_choice == '1':
    x = easy
elif x_choice == '2':
    x = medium
elif x_choice == '3':
    x = hard
else:
    print("Invalid choice!")

Now, y_chars represents the number of characters the user wants in their random string, which should resolve the TypeError you were encountering.

0
Builderboy271 On

I've noticed several issues. First, you are getting the length of the user input, instead of the input. Lines 16 and 17 (according to the image) should be:

X = int(input('Enter Your Choice Here: '))
Y = int(input('Enter Number of Characters Here: '))

Also, lines 27 through 32 should check for an int, instead of a string:

if X == 1:
    X = easy
elif X == 2:
    X = medium
else:
    X = hard

Finally, to print the generated password, you should use this code instead:

for i in range(Y):
    print(rand.choice(X), end = "")

The end = "" means that the next character in the password will be printed on the same line in the console.

This will be the final code:

import os as os
import sys as sys
import random as rand   
import string as str
#FUNCTION FOR THE UI
print('Please choose a difficulty level for your new password:')
print('1. Easy')
print('2. Medium')
print('3. Hard')
X = int(input('Enter Your Choice Here: '))
Y = int(input('Enter Number of Characters Here: '))

easy = str.ascii_lowercase

medium = str.ascii_lowercase + str.ascii_uppercase + str.digits

hard = str.ascii_letters + str.ascii_uppercase + str.digits + str.punctuation

if X == 1:
    X = easy
elif X == 2:
    X = medium
else:
    X = hard

for i in range(Y):
    print(rand.choice(X), end = "")