IfElse Condition doubt

Hello Everyone,
I am trying with simple basic password code for learning with IfElse Condition and I think I was wrong somewhere, but not able to find where I made mistake, If I run the program, it displays the same statement inside “if” condition and “else” statement not displays when I am trying to input with wrong password for checking. So please check the below screenshot and help me out with this.

Hello Kaliswaran,

you are in this forum for several days already. Please stop sending text in pictures. Paste the Python code here as text. It is very difficult to work with text inside pictures.

Put the code between triple backticks (you can insert them using the </> button):

```
# here will be your code
```

Now to your problem:

input(...) is an expression. The text you enter when the program runs is the result of this expression (as a string). Think about what you do with this result of the expression in your program…

1 Like

Hi Kaliswaran

There’s nothing fundamentally wrong with your if else branch, but you could use a different and possibly better flow control method, by using while:, which would loop back and keep asking the user for a password, while ever an incorrect password has been entered.

You’ve failed to use the input() function correctly, which is why your script is not doing as you would like.

Have a read of this:

… and also take on board what @vbrozik has said about posting screen shots, rather than a code block: he does so for very good reason.

Don’t be discouraged; keep learning and post back if you get stuck.

The if: statement is also looking only at the correct password, not comparing the user’s input to it.

Consider what this does…

password = 'Kaliswaran'
if password == 'Kaliswaran':
    print("The password is: Kaliswaran")

Yes, indeed: I was in fact going to use that as a learning point, for when (or if) @Kaliswaran comes back to this thread.

Once he’d read the content of the link I posted, I’m sure he’d of figured out where the issue is.

@Kaliswaran or anyone else that’s interested:

Here’s one solution that solves the issue (which @mlgtechuser kindly pointed out {just having a little fun with you :slight_smile: }) and also obfuscates the password, or rather the passphrase, in this case.

If anyone can crack this, then well done. If anyone want’s to know what passphrase I used, then go right ahead and ask.

from hashlib import sha256

def passcheck():
    passphrase = input('Please enter your passphrase: ')
    hashed_passphrase = sha256(passphrase.encode('utf-8')).hexdigest()
    if hashed_passphrase == 'e569b5938d53ccceddf9afe2aa1761a5501065fef73d413430869905fda79875':
        return 1
    else:
        print('\nInvalid passphrase\n')

valid = ''
while not valid:
    valid = passcheck()

print('\nWell done; you\'re in!!')

{edit for typo}

Cool. The other day I also ran across a reference to a special version of input() that doesn’t show (echo) the keystrokes in the console:

getpass()
1 Like