generating a 8 character password including lower case and upper case and numbers using python -


i want generate password including lower case , upper case , numbers using python should guarantee these 3 kinds has been used. far wrote not guarantee 3 kinds of characters being used. want divide 8 character 2 part. first 3 , last 5. make sure in firs part 3 kinds of character being used shuffle them next part dont know how code that.

import random = 0 password = '' while < 8:     if random.randint(0, 61) < 10:         password += chr(random.randint(48, 57))     elif 10<random.randint(0, 61)<36:         password += chr(random.randint(65, 90))     else:         password += chr(random.randint(97, 122))     += 1 print(password) 

your question consists of 3 parts:

  1. divide 8 character 2 part - first 3 , last 5 - (string slicing)
  2. make sure in first part 3 kinds of character being used (validating passwords)
  3. shuffle characters (shuffling strings)

part1: slicing strings

here's tutorial teaching how slice strings using python..

in case, if insert @ end of code...

print(password[:3]) print(password[3:]) 

... you'll see first 3 chars , last 5.


part2: validating passwords

a answer can found here.

def password_check(password):     # calculating length     length_error = len(password) < 8      # searching digits     digit_error = re.search(r"\d", password) none      # searching uppercase     uppercase_error = re.search(r"[a-z]", password) none      # searching lowercase     lowercase_error = re.search(r"[a-z]", password) none       # overall result     password_ok = not ( length_error or digit_error or uppercase_error or lowercase_error)      return password_ok  password_check(password)  

this function return true if satisfies conditions, , false if not.


part3: shuffling strings

if password_check(password) == true:     new_pwd = ''.join(random.sample(password,len(password)))     print new_pwd 

this code shuffle whole password , assign new variable called new_pwd


ps. whole code can found here!


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -