Back to Freecodecamp

Step 17

curriculum/challenges/english/blocks/learn-regular-expressions-by-building-a-password-generator/65648f4b2281ba50051ae39c.md

latest1.6 KB
Original Source

--description--

Next, write a for loop with i as the loop variable. Use the range() function to iterate up to the value of the length.

Inside the loop, use the addition assignment operator to add a random character from all_characters to the current value of password. Use the choice() function from the secrets module for that.

--hints--

You should write a for loop that iterates over range(length).

js
({ test: () =>
  {
    const transformedCode = code.replace(/\r/g, "");
    const generate_pw = __helpers.python.getDef("\n"+transformedCode, "generate_password");
    const {function_body} = generate_pw;    
    assert(function_body.match(/for\s+i\s+in\s+range\s*\(\s*length\s*\)\s*:/));
  }
})

You should use the += operator to add a random character from all_characters to the current value of password.

js
({ test: () =>
  {
    const generate_pwd = __helpers.python.getDef(code, "generate_password");
    const {function_body} = generate_pwd;    
    assert(function_body.match(/^(\s*)for\s+i\s+in\s+range\s*\(\s*length\s*\)\s*:\s*^\1\1password\s*\+=\s*secrets\.choice\s*\(\s*all_characters\s*\)/m));
  }
})

--seed--

--seed-contents--

py
import secrets
import string

def generate_password(length):
    # Define the possible characters for the password
    letters = string.ascii_letters
    digits = string.digits
    symbols = string.punctuation

    # Combine all characters
    all_characters = letters + digits + symbols
--fcc-editable-region--    
    password = ''
    # Generate password
    
--fcc-editable-region--