Home > Net >  Conditions and If-Statement in Python
Conditions and If-Statement in Python

Time:09-07

I created a small script for generating password in python:

# LIBRARY IMPORTS
from datetime import datetime
import random

# VARIABLES
date = datetime.now()
dateFormat = str(date.strftime("%d-%m-%Y %H:%M:%S"))
lowerCase = "abcdefghijklmnopqrstuvwxyz"
upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "0123456789"
symbols = "!?%&@# */()=<>-_"
passwordConstructor = lowerCase   upperCase   numbers   symbols
minPasswordLength: int = 8
maxPasswordLength: int = 20


# FUNCTIONS
def getUsername():
    global userName
    userName = str(input("Enter Username:"))


def getPasswordLength():
    global passwordLength
    passwordLength = input("Enter the length of password: ")


def generatePassword():
    global password
    password = "".join([random.choice(passwordConstructor) for i in range(passwordLength)])
    print("1."   password)
    password = ''.join(random.sample(password,len(password)))
    print("2."   password)
    password = ''.join(random.sample(password, len(password)))
    print("3."   password)


def generateTextFile():
    if userName != "":
        f = open(userName.upper()   " - "   dateFormat   ".txt", "w ")
        f.write("USERNAME: "   userName   "\nPASSWORD: "   password   "\n\nGENERATED ON: "   dateFormat)
    else:
        f = open("Password generated on "   dateFormat   ".txt", "w ")
        f.write("PASSWORD: "   password   "\n\nGENERATED ON: "   dateFormat)
    f.close()


def printPassword():
    generatePassword()
    print(password)


if getPasswordLength() == '':
        print("Please enter a value. This cannot be empty.")
else:
    if not getPasswordLength().isdigit():
        print("Length of password must be a number.")
    else:
        if getPasswordLength() > maxPasswordLength:
            print('Length of password is limited to '   maxPasswordLength)
        elif getPasswordLength() < minPasswordLength:
            print('Length of password must be grater than '   minPasswordLength)
        else:
            generatePassword()

But condition doesn't work and end up in an error. What I am doing wrong?

Conditions for User Input which should be covered:

  1. Cannot be empty.
  2. Must be number.
  3. Greater than minPasswordLength (8).
  4. Smaller than maxPasswordLength (20).

CodePudding user response:

When you add the max and min password length to the string at the end you must declare them to be a string. It should look like this:

print('Length of password is limited to ' str(maxPasswordLength))

That's the only immediate issue that I can see, however from my experience I know that it has to be done every time an integer or numeric value is added to a string so you will have to amend any other instances where this happens.

Hope this works :)

CodePudding user response:

There are a couple issues here in your code, as others have mentioned, including issues of comparisons between different types, namely comparing the input provided by the user and the min and max password lengths. You also do not have a loop to re-prompt the user to enter a password length again if it does not satisfy the constraints, which I am not sure if it was what you want to achieve. I have enclosed the modified logic in a while loop to keep prompting

# LIBRARY IMPORTS
from datetime import datetime
import random

# VARIABLES
date = datetime.now()
dateFormat = str(date.strftime("%d-%m-%Y %H:%M:%S"))
lowerCase = "abcdefghijklmnopqrstuvwxyz"
upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "0123456789"
symbols = "!?%&@# */()=<>-_"
passwordConstructor = lowerCase   upperCase   numbers   symbols
minPasswordLength: int = 8
maxPasswordLength: int = 20


# FUNCTIONS
def getUsername():
    global userName
    userName = str(input("Enter Username:"))


def getPasswordLength():
    global passwordLength
    passwordLength = input("Enter the length of password: ")


def generatePassword():
    global password
    password = "".join([random.choice(passwordConstructor) for i in range(passwordLength)])
    print("1."   password)
    password = ''.join(random.sample(password,len(password)))
    print("2."   password)
    password = ''.join(random.sample(password, len(password)))
    print("3."   password)


def generateTextFile():
    if userName != "":
        f = open(userName.upper()   " - "   dateFormat   ".txt", "w ")
        f.write("USERNAME: "   userName   "\nPASSWORD: "   password   "\n\nGENERATED ON: "   dateFormat)
    else:
        f = open("Password generated on "   dateFormat   ".txt", "w ")
        f.write("PASSWORD: "   password   "\n\nGENERATED ON: "   dateFormat)
    f.close()


def printPassword():
    generatePassword()
    print(password)

while True:

    getPasswordLength()

    if passwordLength == "":
        print("Please enter a value. This cannot be empty.")
        continue

    try:
        passwordLength = int(passwordLength)
    
    except ValueError:
        print("Length of password must be an integer.")
        continue

    if passwordLength > maxPasswordLength:
        print(f"Length of password is limited to {maxPasswordLength}")
        
    elif passwordLength < minPasswordLength:
        print(f'Length of password must be grater than {minPasswordLength}')
        
    else:
        generatePassword()
        break

  • Related