小编典典

Elif,如果无法正常工作,或者我听不懂

python

好吧,我的代码可以正常工作,但是如果我想重试输入密码时键入No,它将无法正常工作。它只是进入输入密码行(第20行)。我尝试了多种方法来解决此问题,但我根本无法解决。

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)

阅读 221

收藏
2020-12-20

共1个答案

小编典典

if passretry == ‘yes’ or ‘Yes’:

上面的if语句评估为:-

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

现在,由于'Yes'被评估为True,所以您的if陈述始终为True,因此您总是必须输入新密码。


您需要将条件更改为:-

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

同样,以下内容elif应更改为:-

elif passretry in ('no', 'No'):
2020-12-20