Back to Freecodecamp

Step 13

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

latest1.6 KB
Original Source

--description--

Declare a generate_password function and write all your code except the import lines inside the function body.

--hints--

You should declare a function named generate_password.

js
({
  test: () => assert(runPython(`
  _Node(_code).has_function('generate_password')
  `))
})

The import statements should still be outside the function.

js
({
  test: () => assert(runPython(`
  imports_list = _Node(_code).find_imports()
  any([imp.is_equivalent('import secrets') for imp in imports_list]) and any([imp.is_equivalent('import string') for imp in imports_list])
  `))
})

The four variable declarations should be moved inside the function.

js
({
  test: () => assert(runPython(`
  func = _Node(_code).find_function('generate_password')
  vars = ['letters', 'digits', 'symbols', 'all_characters']
  func.find_variable('letters').is_equivalent('letters = string.ascii_letters') and \\
  func.find_variable('digits').is_equivalent('digits = string.digits') and \\
  func.find_variable('symbols').is_equivalent('symbols = string.punctuation') and \\
  func.find_variable('all_characters').is_equivalent('all_characters = letters + digits + symbols') and \\
  all(not _Node(_code).has_variable(var) for var in vars)
  `))
})

--seed--

--seed-contents--

py
import secrets
import string

--fcc-editable-region--
# 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--