Learn Regular Expressions by Building a Password Generator - Step 17

Tell us what’s happening:

The step is " 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."

I’ve tried many things but don’t know exactly what the mistake is. Please help

Your code so far

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


/* User Editable Region */

    # Combine all characters
    all_characters = letters + digits + symbols
    password = ''
    # Generate password
    for i in range(length):
        secrets.choice(all_characters += password)


/* User Editable Region */

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

Learn Regular Expressions by Building a Password Generator - Step 17

add the randomly selected character from all_characters to the password string in each iteration.
[solution redacted]

Actually its showing the problem is in the for loop

Yes, this line inside of the for loop is wrong.

Inside the loop, use the addition assignment operator to add a random character from all_characters to the current value of password.

You need to be adding characters to the password, so the variable password needs to be on the left side of the assignment operator.

Use the choice() function from the secrets module for that.

And, you need to be using secrets.choice() to select the character that is added to the password variable.

I did change but it’s showing this " You should write a for loop that iterates over range(length)"

What’s the updated code?

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
    password = ''
    # Generate password
    for i in range(length):
        secrets.choice(password += all_characters)

Im still a bit confused about the 2nd correction u mentioned
is this correct?

this says

  1. add all_characters to the password

  2. then call secrets.choice() with password as the input

  3. then throw away the value returned by secrets.choice()

That’s not the logic you want, right?

I didn’t know that’s what it meant :sweat_smile:
I don’t think its right then

So what do you want to add to the password variable?

A random character from the all_characters

Ok, so then on the left side of += needs to be the password variable an on the right you need to put the thing you want to add to the password, which is the result from secrets.choice().

1 Like

So maybe like
password += secrets.choice(all_characters) ?

Give it a go! That seems promising.

1 Like

It works now. Thank u so much for helping :smile:

2 Likes

Random characters want to select, but that time use secretes module for the password. Rewrite the phrase like below:

Generate password

Mod Edit SOLUTION REMOVED

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

I apologize for my mistake and thank you for informing me.