0

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)
Paul M.
  • 10,481
  • 2
  • 9
  • 15
mbv2425
  • 3
  • 1
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Random Davis Jan 28 '22 at 16:11
  • `password_count` is *always* a `str` value; if you want an `int`, you need to call `int` on the value yourself. – chepner Jan 28 '22 at 16:11
  • *password_count != type(int)* will always be True because *input()* returns a string object – DarkKnight Jan 28 '22 at 16:12
  • It will never *be* the type `int` nor *have* type `int` (which is what it looks like you are trying to check). – chepner Jan 28 '22 at 16:12

2 Answers2

1

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

Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
  • You should never enter two loops, because one may never leave, It's almost always better to specify an explicit exception type. If you use a naked except: clause, you might end up catching exceptions other than the ones you expect to catch - this can hide bugs or make it harder to debug programs when they aren't doing what you expect. – excitedmicrobe Jan 28 '22 at 16:25
  • @excitedmicrobe Ok so are you saying that I should catch only very specific exceptions? Is there any case in which my code would fail in this scenario? – Riccardo Bucco Jan 28 '22 at 16:28
  • yes, there's `ValueError` – excitedmicrobe Jan 28 '22 at 16:30
  • @excitedmicrobe I know there is `ValueError`, but I don't understand why I should use it in this specific case. Which exceptions other than this should I not catch? – Riccardo Bucco Jan 28 '22 at 16:31
  • I'd suggest a read on [8. Errors and Exceptions](https://docs.python.org/3/tutorial/errors.html) – excitedmicrobe Jan 28 '22 at 16:38
  • @excitedmicrobe I know that part of the documentation. But thanks for not answering my question. I still not understand in which case my code would give the wrong result. Could you please show me one? Also, if you really care about handling only `ValueError` and not other exceptions, please feel free to edit my answer as you desire :) – Riccardo Bucco Jan 28 '22 at 16:52
0

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
excitedmicrobe
  • 2,338
  • 1
  • 14
  • 30