如何在 python 的循环中正确修复语法错误 'continue'
How to fix syntax error 'continue' not properly in a loop in python
I want to create a simple database, which will store and return data.
问题是 continue
似乎不起作用。
一旦 function checker
中的 else statement
被执行,它就会继续打印 "try again" 并且似乎陷入僵局。
def checker():
#Checks if the password is strong enough
while True:
if (p[0].isupper() and not p.isalpha() and len(p) > 7):
print('Password Created!')
break
#If password is not strong enough try again
else:
print('Try Again')
continue
#Starts main program
while True:
database = {'dom':'yeet'}
welcome = input('Welcome! To login press L, if you are new press R: ')
#Login will log basically check if your info is in dictionary
if welcome == 'L':
for user in database.keys():
u = input('Username: ')
if u in user:
for passw in database.values():
p = input('Password: ')
if p in passw:
print('Welcome ' + str(u))
break
#Will create new user in dictionary
elif welcome == 'R':
u = input('Choose your username: ')
p = input('Choose your password: ')
#Runs checker function
checker()
database[u] = p
print('Welcome ' + str(u))
break
else:
continue
像这样重写 checker() :
您对 else 块使用了不正确的缩进,它超出了循环范围。
def checker():
#Checks if the password is strong enough
while True:
if (p[0].isupper() and not p.isalpha() and len(p) > 7):
print('Password Created!')
break
else:
print('Try Again')
continue
Python 编码的美妙之处在于您必须正确地缩进代码,牢记这一点,每个人都会自动编写漂亮的代码,并且您永远不会看到格式错误的 Python 代码。
缩进你的其他部分,你很高兴。
I want to create a simple database, which will store and return data.
问题是 continue
似乎不起作用。
一旦 function checker
中的 else statement
被执行,它就会继续打印 "try again" 并且似乎陷入僵局。
def checker():
#Checks if the password is strong enough
while True:
if (p[0].isupper() and not p.isalpha() and len(p) > 7):
print('Password Created!')
break
#If password is not strong enough try again
else:
print('Try Again')
continue
#Starts main program
while True:
database = {'dom':'yeet'}
welcome = input('Welcome! To login press L, if you are new press R: ')
#Login will log basically check if your info is in dictionary
if welcome == 'L':
for user in database.keys():
u = input('Username: ')
if u in user:
for passw in database.values():
p = input('Password: ')
if p in passw:
print('Welcome ' + str(u))
break
#Will create new user in dictionary
elif welcome == 'R':
u = input('Choose your username: ')
p = input('Choose your password: ')
#Runs checker function
checker()
database[u] = p
print('Welcome ' + str(u))
break
else:
continue
像这样重写 checker() : 您对 else 块使用了不正确的缩进,它超出了循环范围。
def checker():
#Checks if the password is strong enough
while True:
if (p[0].isupper() and not p.isalpha() and len(p) > 7):
print('Password Created!')
break
else:
print('Try Again')
continue
Python 编码的美妙之处在于您必须正确地缩进代码,牢记这一点,每个人都会自动编写漂亮的代码,并且您永远不会看到格式错误的 Python 代码。 缩进你的其他部分,你很高兴。