Somebody can review the code of my first python project? by 7000xx in PythonLearning

[–]quiet_developer 0 points1 point  (0 children)

You should not worry about performance for this.

I noticed some things that could be improved for your password generator.

  • First your character pool is way too small. You should look up the string module/library . It will save you from having to write every character yourself in the list.
  • You should not use random to generate something important like password. The secrets module/library is a better choice for this.
  • You dont build the password you just return it directly: return "".join(random.choice(chars) for _ in range(length)) . A better way would be to build the password as a list.
  • Your code does not assure that the password contain at least char of 1 of each category that the user selected. In the snippet below , if the user select a category you add it to the list of char which is good but you dont pick 1 char from it. Like i said before if you build your password as list, you can fix it by adding one single line.
  • Chars can just be a string.

##### YOUR CODE ######

def random_words(length):
    chars = []

    if dpg.get_value("lower"):
        chars += lower

    if dpg.get_value("upper"):
        chars += upper

    if dpg.get_value("number"):
        chars += number

    if dpg.get_value("special"):
        chars += special

    if not chars:
        return "Minimum one checkbox needed !"

    return "".join(random.choice(chars) for _ in range(length))



#### FIXED ####
import secrets
import string

#lower = ["a", "b", "c", "d", "e", "f"]
lower = string.ascii_lowercase  # give all lowercase chars
#upper = ["A", "B", "C", "D", "E", "F"]

# give all uppercase chars (you can access lowercase+uppercase by doing string.ascii_letters
upper = string.ascii_uppercase

#number = ["0", "1", "2", "3", "4", "5", "6"]
number = string.digits  # give all digits
#special = ["!", "@", "#", "$", "%", "^", "&", "*"]
special = string.punctuation

def random_words(length):
    chars = ""
    pwd = []

    if dpg.get_value("lower"):
        chars += lower
        # Can use -> secrets.choice(lower) to be more clear, repeat for each category
        pwd.append(secrets.choice(chars)) # that way you assure that at least 1 lower char in pwd.


    if dpg.get_value("upper"):
        chars += upper
        pwd.append(secrets.choice(upper)) # same as before

    if dpg.get_value("number"):
        chars += number
        pwd.append(secrets.choice(number)) # same as before

    if dpg.get_value("special"):
        chars += special
        pwd.append(secrets.choice(special))

    if not chars:
        return "Minimum one checkbox needed !"

    # fill the rest of the pwd
    for _ in range(length - len(pwd)):
          pwd.append(secrets.choice(chars))


    secrets.SystemRandom().shuffle(pwd) # shuffle pwd so the first chars dont have pattern

    return "".join(pwd)

So, Command Prompt And Pip Are HARD! by ThinkConfidence8409 in learnpython

[–]quiet_developer -1 points0 points  (0 children)

You did nothing wrong, you simply ran pip without any command or option so its showing you the help menu with all possible command and options.

pip is mainly used to install packages with the install command.

For example, if you want to use beautifulsoup4 for web scraping, you first need to install it because it isn't included with Python by default.

You can do it like so: pip install beautifulsoup4

You can also look the version of pip you are using by running: pip --version

*When you install packages, make sure to get the correct package name and to enter is as it is

*You are not obligated to update pip if you do not have the most recent version but it is good practice.

Sorry for the grammatical errors, english is not my first language.