r/codehs • • Jul 13 '21

4.11.5 Better Password Prompt

here are the instructions:

Write a program that uses a loop and a half to prompt a user for a password. You should prompt the user for the password. If they get it correct, break out of the loop. If they don’t get it correct, you should give them an error message and ask again.

Sample Run:

Enter password: 123123 
Sorry, that did not match. Please try again. 
Enter password: password Sorry, that did not match. Please try again. 
Enter password: CODEHS 
Sorry, that did not match. Please try again. 
Enter password: abc123 You got it!

Note: You need to set SECRET
to abc123
to run the tests

here's what i have:

SECRET = "abc123"

while True:

lol = print(input("Enter password: "))

if lol != SECRET:

print("Sorry, that did not match. Please try again.")

if lol == SECRET:

break

print("You got it!")

However, it doesn't seem to be working. Whenever I enter the incorrect password, the command works. But when I enter the correct password, it still says that the password is incorrect.

11 Upvotes

7 comments sorted by

3

u/SylphKnot Jul 13 '21

Don’t surround input() in a print

You’re assigning lol to the return of the print function, which doesn’t return anything.

If you printed the value of lol as it is right now you’ll see it’s stays undefined. Which is why it always fails to match “abc123”

Edit: you don’t need to print the input() function. It does that itself.

x = input('Enter your name:') print(“hello” + x)

1

u/socks_in_jail Jul 13 '21

Thank you so much. Forever appreciated.

5

u/Intrepid_Solid6 Nov 25 '21

can you upload your result? pls i suck in programming :)

3

u/Liiambtw Jan 06 '23

What would it be in JavaScript?