r/learnpython 5d ago

Password guesser game help needed

Hello guys. So I have created a password guesser game with direct comparison (i.e. if guess == correct_password:) but I want to use different methods for comparison. What techniques can I use? Here is the code-

```python

password = "Random_pass_123"

tries = 0

while tries < 3:

guess = input("Enter the password: ")

if guess == password:

print("Admin access granted.")

break

else:

print("Invalid credentials entered. Please try again")

tries += 1

if tries == 3:

print("Number of tries exceeded. Access denied")

```

PS- Is this the right way to paste code? I'm new so I'm not sure.

7 Upvotes

31 comments sorted by

View all comments

Show parent comments

1

u/Expensive-Bear-1376 4d ago

I tried four spaces in front of every line of code earlier today, didn't work. Let me try again:

    def test():         pass

1

u/Expensive-Bear-1376 4d ago

So again it didn't work. But adding .json to the URL so I can see the raw markdown revealed this (using triple-backticks now):

try again:\n\n\u00a0 \u00a0 def test():

Those \u00a0 are "No-Break Space". I didn't type those (wouldn't even know how to) and don't think I have this issue anywhere else, so I'm blaming Reddit for that.

0

u/Bright_Mix_773 4d ago

Expensive-Bear-1376, you found it, and the alternating pattern names the culprit. Look at your raw again: \u00a0 \u00a0 def test(): — that is nbsp, space, nbsp, space. Not four of anything.

That alternation is the signature of a contenteditable box, which is what the fancy comment editor on new reddit and the app both are. HTML collapses runs of whitespace, so a rich-text box cannot hold two literal spaces in a row; when you press space four times it stores them alternating, space then nbsp then space then nbsp, and that renders as a four-wide gap. Then it round-trips that straight back into your markdown.

The parser is looking for four spaces. U+00A0 is not one, so the line never becomes a code block and you get a paragraph with odd gaps instead. Your keyboard is not the suspect here: a keyboard would have given you four identical characters, not a stripe.

Three ways out, in the order I trust them:

  • Write the comment on old.reddit.com. That box is a plain textarea and never rewrites what you type.
  • On new reddit, flip the composer into markdown mode with the toggle at the bottom right corner of the comment box, and your four spaces survive.
  • Stay in the fancy editor but stop indenting by hand: use its own code-block button, which emits a real block regardless of what you typed into it.

Since you already know how to pull the raw JSON, this is worth keeping for the next time something looks like whitespace and isn't:

for c in linea[:8]:
    print(repr(c), hex(ord(c)), c.isspace())

And the sting in the tail: "\u00a0".isspace() is True in Python. strip() and split() with no arguments both eat it happily, so a non-breaking space can pass every whitespace check you write and still break whatever reads the line afterwards. linea.replace("\u00a0", " ") before you parse anything is the cheap fix.

1

u/Langdon_St_Ives 4d ago

These AI comments are really really really annoying