0

Well my code is working, but when I type No if I want to retry to enter the password it doesn't work; it just goes to the enter password line (line 20). I have tried multiple ways to fix this but I simply cannot.

import time
import os

print ("Hello world.")
time.sleep(1)
print ("Waiting 5 seconds.")
time.sleep(5)
print ("You have waited 10 seconds.")
print ("Executing Chrome.")
time.sleep(1)
print ("Execution failed!")
password = input("Enter the execution password: ")
if password == 'password1234':
    os.system ('C:\\Users\\Harry\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe')
else:
    print ("Wrong password!")
    time.sleep(1)
    passretry = input("Do you want to try again? ")
    if passretry == 'yes' or 'Yes':
        passretry1 = input("Enter password: ") 
        if passretry1 == 'password1234':
            os.system ('C:\\Users\\Harry\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe')
    elif passretry == 'no' or 'No':
        print ("Closing...")
        time.sleep(1)
    else:
        print ("Wrong password.")
        time.sleep(.5)
        print ("Retry limit exceeded, closing.")
        time.sleep(1)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
haws1290
  • 105
  • 1
  • 9
  • 2
    Someone needs to make a list of common Python misconceptions. The topic of `if a == x or y` would be on the top of the list. – Mark Ransom Jan 31 '13 at 22:46
  • @MarkRansom: There are already a few such lists (even the FAQ sort of has one), but for some reason this doesn't seem to be on them… – abarnert Jan 31 '13 at 23:51
  • @MarkRansom - it's not just python where this misconception is common – KevinDTimm Feb 01 '13 at 13:31

3 Answers3

9
if passretry == 'yes' or 'Yes':

the above if statement is evaluated as: -

if (passretry == 'yes') or 'Yes':

Now, since 'Yes' is evaluated to True, so, your if statement is always True, and hence you always have to enter new password.


You need to change the condition to: -

if passretry in ('yes', 'Yes'):

likewise, the following elif should be changed to: -

elif passretry in ('no', 'No'):
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
3

This condition:

if passretry == 'yes' or 'Yes':

means "If passretry == 'yes' is true, or 'Yes' is true". 'Yes' is always true, because a non-empty string counts as true. That's why you're always taking the first code path.

You need to spell things out a little more:

if passretry == 'yes' or passretry == 'Yes':

(Or to make your code a bit more general:

if passretry.lower() == 'yes':

which would allow for people shouting YES.)

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
2

You need another complete statement:

passretry == 'yes' or passretry == 'Yes':

The string 'Yes' always evaluates to True.

matt snider
  • 4,013
  • 4
  • 24
  • 39