1

Prompt for python program: 5.16 LAB: Password modifier Many user-created passwords are simple and easy to guess. Write a program that takes a simple password and makes it stronger by replacing characters using the key below, and by appending "!" to the end of the input string.

i becomes 1 a becomes @ m becomes M B becomes 8 s becomes $ Ex: If the input is:

mypassword the output is:

Myp@$$word! Hint: Python strings are immutable, but support string concatenation. Store and build the stronger password in the given password variable. My code so far:

word = input()
password = ''
i = 0
while i < len(word):

I'm really just confused about changing certain characters in a string using a while loop. I was also trying to use if statements. While loops are hard for me understand. I don't know where to start.

1 Answer 1

3

You're doing fine. While loops have this format:

while (condition) == True: 
    # do this

In your case, you will want to put i += 1 at then end of your loop so it doesn't go on forever, like this:

while i < len(word):
    # your code here

    i += 1 # this adds 1 to i
           # so eventually it will be equal to the length of word, ending the loop

As for if statements, you could try this:

while i < len(word):
    if word[i] == 'i':
        password += '1'
    
    i += 1

The if statements will only work on the values you want to change, so add an else statement to act as a default.

while i < len(word):
    if word[i] == 'i':
        password += '1'
    else:
        password += word[i]
    
    i += 1

Now, if you get a normal letter, like h, it will add it to the password.

Hope this helps you get started!

Sign up to request clarification or add additional context in comments.

3 Comments

That helped a lot, I put the other if statements below the first one same formatting but using elif. Then I put a print statement after i += 1 saying print(password + '!') but when I use password as the input the output is just M@$$!
There you go. You can also use else: as a default for normal values. Have a nice day :).
I'll explain it in the answer :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.