I am currently working on a project where we have to check password strength and generate a strong password, but In the task of making a way for the program to exit I've hit a block, and am unable to find ways to progress. Ive tried break and sys.exit() but neither seem to work.
I would like to, when they enter [3] then ['yes'], for the program to end, but it just returns to the first question.
I have also attempted the while = True: , but that had even less success.
count = 0
while (count < 1):
while True:
choice = input ("Do you want to: 1) Check a password 2) Generate a Password, or 3) Quit? . [1/2/3]? : ")
if choice in ['1', '2', '3']:
break
if choice == "1":
while True:
checkyes = input ("you want to check a password, correct? [yes/no]")
if checkyes in ['yes', 'no']:
break
elif choice == "2":
while True:
genyes = input ("you want to generate a password, correct? [yes/no]")
if genyes in ['yes', 'no']:
break
else:
while True:
quityes = input ("you want to quit, correct? [yes/no]")
if quityes in ['yes', 'no']:
break
if choice == "yes":
count = count + 1
else:
pass
3 Answers 3
You are checking the wrong variable for the exit condition. You need to check with quityes:
count = 0
while (count < 1):
while True:
choice = input ("Do you want to: 1) Check a password 2) Generate a Password, or 3) Quit? . [1/2/3]? : ")
if choice in ['1', '2', '3']:
break
if choice == "1":
while True:
checkyes = input ("you want to check a password, correct? [yes/no]")
if checkyes in ['yes', 'no']:
break
elif choice == "2":
while True:
genyes = input ("you want to generate a password, correct? [yes/no]")
if genyes in ['yes', 'no']:
break
else:
while True:
quityes = input ("you want to quit, correct? [yes/no]")
if quityes in ['yes', 'no']:
if quityes == 'yes'
count += 1
break
else:
pass
Output:
Do you want to: 1) Check a password 2) Generate a Password, or 3) Quit? . [1/2/3]? : 3
you want to quit, correct? [yes/no]yes
Process finished with exit code 0
Comments
2 problems:
- the break statement is executed before count in incremented
you check
choiceinsteadquityesagainst'yes'if quityes in ['yes', 'no']: if quityes == "yes": count = count + 1 else: pass break
Comments
You can try to import sys and replace the break with sys.exit().
Think the reason why the break didnt work is because its breaking out of first loop but not out of the bigger loop.
breakis beforecount = count + 1- that's the problem.