Python Forum

Full Version: need help with input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have written / modified a random password generator, and I need help with 1 part .

The part I need help with is:
if the user presses enter (leaves it blank) for the length of the password
then it would go to default value of 20
otherwise what ever the user enters. eg 10

it would need to be an integer for the rest of the program to work properly.

any help would be appreciated.

this is the code below for python3

import string
import random


## characters to generate password from
alphabets = list(string.ascii_letters)
digits = list(string.digits)
special_characters = list("!@#$%^&*()")
characters = list(string.ascii_letters + string.digits + "!@#$%^&*()")


def random_password():
    ## length of password from the user
    print('Default password length is 20')
    length = int(input("Enter password length or press enter for default password length: "))

    length = sum([length,1])
     
    print((length)-1)
    ## number of character types
    alphabets_count = int(input("Enter alphabets count in password: "))
    while alphabets_count > length:
                print('Alphabets count is greater than password length')
                alphabets_count = int(input("Enter alphabets count in password: "))
    else:
                
                dlength = (length - alphabets_count -1)
                digits_count = int(input("Enter digits count in password: "))
                while digits_count > dlength:
                    print('Digits Count is greater than password length')
                    print('Enter ' + str(dlength) + ' digits or less')
                    digits_count = int(input("Enter digits count in password: "))
                else:
                
                    sclength = (length - alphabets_count - digits_count -1)
                    sclengthcalc = (length - alphabets_count - digits_count)
                    special_characters_count = int(input("Enter special characters count in password: "))
                    while special_characters_count > sclengthcalc:
                        print('Special characters Count is greater than password length')
                        print('Enter ' + str(sclength) + ' special characters or less')
                        special_characters_count = int(input("Enter special characters count in password: "))
                
                    else:
                        pass
                pass
        
                    
    characters_count = alphabets_count + digits_count + special_characters_count

    
    # initializing the password
    password = []
    
    # picking random alphabets
    for i in range(alphabets_count):
        password.append(random.choice(alphabets))


    ## picking random digits
    for i in range(digits_count):
        password.append(random.choice(digits))


    ## picking random alphabets
    for i in range(special_characters_count):
        password.append(random.choice(special_characters))


    # if the total characters count is less than the password length
    # add random characters to make it equal to the length
    if characters_count < length:
        random.shuffle(characters)
        for i in range(length - characters_count -1):
            password.append(random.choice(characters))


    ## shuffling the resultant password
    random.shuffle(password)

    ### converting the list to string
    ### printing the list
    print("".join(password))

### invoking the function
random_password()
If they leave it blank, input will return an empty string, so you can check that. You can write an empty string literal as "".
ndc85430 is correct but you also cannot cast an empty string to anint. Try it like this:
import string
import random
 
 
## characters to generate password from
alphabets = list(string.ascii_letters)
digits = list(string.digits)
special_characters = list("!@#$%^&*()")
characters = list(string.ascii_letters + string.digits + "!@#$%^&*()")
 
 
def random_password():
	## length of password from the user
	print('Default password length is 20')

	length = input("Enter password length or press enter for default password length: ")
	if length == '' :
		length = 20 
	else :
		length = int(sum([length,1]))
	  
	print((length)-1)
	## number of character types
	alphabets_count = int(input("Enter alphabets count in password: "))
	while alphabets_count > length:
				print('Alphabets count is greater than password length')
				alphabets_count = int(input("Enter alphabets count in password: "))
	else:
				 
				dlength = (length - alphabets_count -1)
				digits_count = int(input("Enter digits count in password: "))
				while digits_count > dlength:
					print('Digits Count is greater than password length')
					print('Enter ' + str(dlength) + ' digits or less')
					digits_count = int(input("Enter digits count in password: "))
				else:
				 
					sclength = (length - alphabets_count - digits_count -1)
					sclengthcalc = (length - alphabets_count - digits_count)
					special_characters_count = int(input("Enter special characters count in password: "))
					while special_characters_count > sclengthcalc:
						print('Special characters Count is greater than password length')
						print('Enter ' + str(sclength) + ' special characters or less')
						special_characters_count = int(input("Enter special characters count in password: "))
				 
					else:
						pass
				pass
		 
					 
	characters_count = alphabets_count + digits_count + special_characters_count
 
	 
	# initializing the password
	password = []
	 
	# picking random alphabets
	for i in range(alphabets_count):
		password.append(random.choice(alphabets))
 
 
	## picking random digits
	for i in range(digits_count):
		password.append(random.choice(digits))
 
 
	## picking random alphabets
	for i in range(special_characters_count):
		password.append(random.choice(special_characters))
 
 
	# if the total characters count is less than the password length
	# add random characters to make it equal to the length
	if characters_count < length:
		random.shuffle(characters)
		for i in range(length - characters_count -1):
			password.append(random.choice(characters))
 
 
	## shuffling the resultant password
	random.shuffle(password)
 
	### converting the list to string
	### printing the list
	print("".join(password))
 
### invoking the function
random_password()
Style issue here, I would move the input outside of the function. The idea being that a function should serve a single purpose - generating the password. You can then have the function use a named parameter with a default. Lots of options. Below picks up everything except it will accept a negative number for the length. I also did not duplicate your password function

import random
import string

def random_password(pwd_len = 20) :
    return 'asdffdsa'

ok=False
while not ok:
    len_of_pwd = input('Length of password (default=20): ')
    if len(len_of_pwd) == 0 :
        ok = True
        mypass= random_password()
    else :
        try :
            mypass=random_password(int(len_of_pwd))
            ok = True
        except :
            print('Must enter an integer')
print(f'Password = {mypass}')
I have fixed it so that it now works.
Thanks