After the phone number is confirmed to be incorrect, and when the userTries and passTries is more than 2, the code still continues. Is there a way to stop this? I want the code to end where it says break. But it doesn’t. The code in the while True loop still runs after it.
import time
userTries = 0
passTries = 0
while True:
# welcome user
print('Welcome to [WEBSITE].')
print('You must create an account to continue.')
# variable inputs
time.sleep(1)
username = input('Please enter a username: ')
password = input('Please enter a password: ')
phonenum = int(input('Please enter your phone number for user confirmation: '))
# account creation confirmation
time.sleep(1)
print('Welcome,', username '.')
print('Thank you for creating your account!')
# re-enter username password
time.sleep(1)
usernameQ = input('Please re-enter your username: ')
while usernameQ != username:
# username checker
userTries = 1
time.sleep(1)
newusernameQ = input('Username incorrect, would you like to make a new username? (y or n) ')
# (not) creating new username
while newusernameQ not in ('Y', 'y', 'N', 'n'):
time.sleep(1)
print('Invalid answer.')
newusernameQ = input('Would you like to make a new username? (y or n)')
# phone number confirmation
if newusernameQ in ('Y', 'y'):
phonenumberQ = int(input('What is your phone number? '))
if phonenumberQ != phonenum:
time.sleep(1)
print('Phone number incorrect. Account frozen.')
break
else:
# re doing username
time.sleep(1)
username = input('Please enter a new username: ')
time.sleep(1)
print('Username set.')
time.sleep(1)
usernameQ = input('Please re-enter your username: ')
else:
# username ask
usernameQ = input('Please re-enter your username: ')
# tries too many
if userTries > 2:
time.sleep(1)
print('Too many attempts, account frozen.')
break
# asking for first password
time.sleep(1)
passwordQ = input('Please re-enter your password: ')
# password checker
while passwordQ != password:
passTries = 1
time.sleep(1)
newpasswordQ = input('Password incorrect, would you like to make a new password? (y or n) ')
# (not) creating new password
while newpasswordQ not in ('Y', 'y', 'N', 'n'):
time.sleep(1)
print('Invalid answer.')
newpasswordQ = input('Would you like to make a new password? (y or n)')
# phone number confirmation
if newpasswordQ in ('Y', 'y'):
phonenumberQ = int(input('What is your phone number? '))
if phonenumberQ != phonenum:
time.sleep(1)
print('Phone number incorrect. Account frozen.')
break
else:
# re doing password
time.sleep(1)
password = input('Please enter a new password: ')
time.sleep(1)
print('Password set.')
time.sleep(1)
passwordQ = input('Please re-enter your password: ')
else:
# password ask
passwordQ = input('Please re-enter your password: ')
# tries too many
if passTries > 2:
time.sleep(1)
print('Too many attempts, account frozen.')
break
if passwordQ == password and usernameQ == username:
for i in range(2):
time.sleep(1)
print('Logging in...')
time.sleep(1)
print('Login Successful.')
time.sleep(1)
print('Welcome, ' username '.')
break
CodePudding user response:
You need to understand, that break only breaks out of your first while loop, not the main while True
loop
Here is a simple example and mulitple choices for how to end the loop
The variable something_other
is representing what ever other state you want to check to keep your while loop running.
if running becomes False, the loops will run to the end and then print 'done'
count = 0
running = True
something_other = True
while running:
while something_other and running:
print('start of while')
count = 1
if count > 2:
running = False
print('end of while')
print('last line of outer loop')
print('done')
Here running will become False and the inner loop will break and not print 'end of while' but the outer loop will run to the end and print 'last line of outer loop'
count = 0
running = True
something_other = True
while running:
while something_other and running:
print('start of while')
count = 1
if count > 2:
running = False
break
print('end of while')
print('last line of outer loop')
print('done')
And the last one will even break the outer loop as it checks for running after the inner loop is finished or exited with a break
count = 0
running = True
something_other = True
while running:
while something_other and running:
print('start of while')
count = 1
if count > 2:
running = False
break
print('end of while')
if not running:
break
print('last line of outer loop')
print('done')
CodePudding user response:
If you want to fully stop your program, consider using sys.exit()
Otherwise, a break statement only stops the most inner loop, the outer ones still continue. Thus, you need to add one break for each loop you want to stop.
Alternatively, you can define a boolean variable called "stop" and add it in your 'while' conditions. Something like:
while (usernameQ != username) and (not stop):