Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
  1. Please read documentation, random has a usage warning at the top of the documentation:

    Warning: The pseudo-random generators of this module should not be used for security purposes. Use os.urandom() or SystemRandom if you require a cryptographically secure pseudo-random number generator.

  2. Use string formatting, this simplifys the creation of strings.

  3. Have a standard for your indentation. I'm honestly surprised this code runs with your tabbing.

  4. You can simplify your equality checks, rather than doing a == b or a == c, you can use a in {b, c}. I use a set due to the peephole optimization.

  5. Use loops, if you use for _ in range(amount) you can reduce your code significantly.

  6. Move duplicate code into one function, in this case move your prints of the password out of; small, med, and big. And put it in generate.

  7. Make a single function to be able to make any sized input.

  8. Change generate to account for this.

  9. Use a main. You want to keep things out of global scope, so it's as small as possible. This can lead to performance improvements This can lead to performance improvements.

  10. Protect your main function with a if __name__ == '__main__': guard if __name__ == '__main__': guard.

  11. You can simplify the for loop to a list comprehension, this documentation may be easier to understand this documentation may be easier to understand.

  12. You can define your letters as a global constant, so that if you do need them again later, you can use the one we've defined. And so if at a later date you want to add or remove letters, then you can without having to change multiple strings.

  1. Please read documentation, random has a usage warning at the top of the documentation:

    Warning: The pseudo-random generators of this module should not be used for security purposes. Use os.urandom() or SystemRandom if you require a cryptographically secure pseudo-random number generator.

  2. Use string formatting, this simplifys the creation of strings.

  3. Have a standard for your indentation. I'm honestly surprised this code runs with your tabbing.

  4. You can simplify your equality checks, rather than doing a == b or a == c, you can use a in {b, c}. I use a set due to the peephole optimization.

  5. Use loops, if you use for _ in range(amount) you can reduce your code significantly.

  6. Move duplicate code into one function, in this case move your prints of the password out of; small, med, and big. And put it in generate.

  7. Make a single function to be able to make any sized input.

  8. Change generate to account for this.

  9. Use a main. You want to keep things out of global scope, so it's as small as possible. This can lead to performance improvements.

  10. Protect your main function with a if __name__ == '__main__': guard.

  11. You can simplify the for loop to a list comprehension, this documentation may be easier to understand.

  12. You can define your letters as a global constant, so that if you do need them again later, you can use the one we've defined. And so if at a later date you want to add or remove letters, then you can without having to change multiple strings.

  1. Please read documentation, random has a usage warning at the top of the documentation:

    Warning: The pseudo-random generators of this module should not be used for security purposes. Use os.urandom() or SystemRandom if you require a cryptographically secure pseudo-random number generator.

  2. Use string formatting, this simplifys the creation of strings.

  3. Have a standard for your indentation. I'm honestly surprised this code runs with your tabbing.

  4. You can simplify your equality checks, rather than doing a == b or a == c, you can use a in {b, c}. I use a set due to the peephole optimization.

  5. Use loops, if you use for _ in range(amount) you can reduce your code significantly.

  6. Move duplicate code into one function, in this case move your prints of the password out of; small, med, and big. And put it in generate.

  7. Make a single function to be able to make any sized input.

  8. Change generate to account for this.

  9. Use a main. You want to keep things out of global scope, so it's as small as possible. This can lead to performance improvements.

  10. Protect your main function with a if __name__ == '__main__': guard.

  11. You can simplify the for loop to a list comprehension, this documentation may be easier to understand.

  12. You can define your letters as a global constant, so that if you do need them again later, you can use the one we've defined. And so if at a later date you want to add or remove letters, then you can without having to change multiple strings.

Source Link
Peilonrayz
  • 44.4k
  • 7
  • 80
  • 157
  1. Please read documentation, random has a usage warning at the top of the documentation:

    Warning: The pseudo-random generators of this module should not be used for security purposes. Use os.urandom() or SystemRandom if you require a cryptographically secure pseudo-random number generator.

  2. Use string formatting, this simplifys the creation of strings.

  3. Have a standard for your indentation. I'm honestly surprised this code runs with your tabbing.

  4. You can simplify your equality checks, rather than doing a == b or a == c, you can use a in {b, c}. I use a set due to the peephole optimization.

  5. Use loops, if you use for _ in range(amount) you can reduce your code significantly.

  6. Move duplicate code into one function, in this case move your prints of the password out of; small, med, and big. And put it in generate.

  7. Make a single function to be able to make any sized input.

  8. Change generate to account for this.

  9. Use a main. You want to keep things out of global scope, so it's as small as possible. This can lead to performance improvements.

  10. Protect your main function with a if __name__ == '__main__': guard.

  11. You can simplify the for loop to a list comprehension, this documentation may be easier to understand.

  12. You can define your letters as a global constant, so that if you do need them again later, you can use the one we've defined. And so if at a later date you want to add or remove letters, then you can without having to change multiple strings.

import random
r = random.SystemRandom()
r.choice()
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz"
def choice_over(letters, amount):
 choice = r.choice
 return ''.join(choice(letters) for _ in range(amount))
def generate():
 print("How big do you want your password?")
 while True:
 try:
 choice = int(input("please input the length >> "))
 break
 except ValueError:
 print('Please enter a number.')
 generatepassword = choice_over(LETTERS, choice)
 print(generatepassword)
 print("The passwords consists of: {} Characters".format(len(generatepassword)))
 print("\n")
def main():
 again = 'yes'
 while again in {'yes', 'y'}:
 generate()
 print("\n")
 print("Do you want to generate another password? [yes] or [no] >> ")
 again = input()
if __name__ == '__main__':
 main()
lang-py

AltStyle によって変換されたページ (->オリジナル) /