Home > other >  Building a password generator
Building a password generator

Time:01-29

I have recently built a password generator but wanted to include an aspect where if the user types in a letter instead of a number when defining the length of the password and number of passwords then the output would be to loop back in. If not the password generator would continue if numbers were inputted.

This is my code so far:

import random

char = "abcdefghijklmnopqrstuvwkyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!@£$%^&*"

while True:

   
        password_length = input("how long do you want your password? ")
        password_count = input("how many passwords do you want? ")


        if password_length and password_count != type(int):

            print("Please can you enter a number")

        elif password_length and password_count == type(int):

            for x in range(0,int(password_count)):

                password = ""
                    
                for y in range(0,int(password_length)):

                    random_letters = random.choice(char)
                    password  = random_letters

                print(password)

CodePudding user response:

Try this:

while True:
    try:
        password_length = int(input("how long do you want your password? "))
    except:
        print("Invalid input")
    else:
        break

while True:
    try:
        password_count = int(input("how many passwords do you want? "))
    except:
        print("Invalid input")
    else:
        break

And then you can go on with the rest of your code

CodePudding user response:

Python offers a better way to check if a string is a digit or not.

From w3:

The isdigit() method returns True if all the characters are digits, otherwise False.

import random

char = "abcdefghijklmnopqrstuvwkyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!@£$%^&*"
while True:
   password_length = input("how long do you want your password? ")
   password_count = input("how many passwords do you want? ")

   if password_count.isdigit() and password_length.isdigit(): 
     password_int = int(password_count)
     password = ""
     for x in range(0,password_int):
         for y in range(0,int(password_length)):
            random_letters = random.choice(char)
            password  = random_letters
           
     print(password)
   else: 
      print("Please enter a valid input in numbers")

Output:

how long do you want your password? 8
how many passwords do you want? 1

ABZEG66j

CodePudding user response:

this code is not giving any output

  •  Tags:  
  • Related