使用布尔标志、逻辑和缩进错误中断 python for 循环
Break out of a python for loop using boolean flag, logic and indentation error
我有以下代码,用于搜索用户名列表,并在第一次输出时简单地 return 输出 "Found in index i" 或 "Sorry username not found"。
usernames=["u1","u2","u3"]
found=False
while found==False:
username=input("Enter username:")
for i in range(len(usernames)):
if username==usernames[i]:
found=True
break
if found==True:
print("Username found in index:",i)
else:
print("Sorry,username not found")
如果用户名正确,当前代码似乎可以工作,但如果使用了错误数据,例如 23234,那么它会重复问题并且不会跳转到代码底部的 if 语句(这就是我想要的) .
有人可以更正此代码并说明解决此问题的最有效方法。这可能与布尔标志 'found' 有关,我不明白为什么它没有爆发并进入底部 if 语句。提前致谢
while found==False:
使其循环,直到 found
变为 True
。
因此,如果它没有找到您正在寻找的用户名,它会循环并再次询问您。
此外,如果您想查看列表中是否存在某个字符串,只需使用 list.index()
method:
username=input("Enter username:")
try:
i = usernames.index(username)
except ValueError:
print("Sorry,username not found")
else:
print("Username found in index:",i)
您不需要那些布尔标志、基于范围的循环或额外的 if 条件:
usernames=["u1","u2","u3"]
while True:
user = input("Enter username: ")
if user in usernames:
print("Username found at Index: {}".format(usernames.index(user)))
break
else:
print("Sorry, username not found. Try again")
编辑:
但是,如果您必须继续当前使用 for 循环的方法,请在外部 for 循环中放置一个 else 块,如果找到则中断:
usernames = ["u1","u2","u3"]
found = False
while found == False:
username = input("Enter username: ")
for i in range(len(usernames)):
if username == usernames[i]:
print("Username found at Index: {}".format(i))
break
else: # not and indentation error
print("Sorry, username not found. Try again")
编辑 2:(没有布尔标志)
usernames = ["u1","u2","u3"]
while True:
username = input("Enter username: ")
for i in range(len(usernames)):
if username == usernames[i]:
print("Username found at Index: {}".format(i))
break
else: # not and indentation error
print("Sorry, username not found. Try again")
OUTPUT(在所有情况下):
Enter username: 2334
Sorry, username not found. Try again
Enter username: u2
Username found at Index: 1
如果输入正确的用户名,您的代码将永远不会到达第二个,因为它被设计为仅到达第二个 if
。
您必须决定是继续询问用户名直到输入正确的用户名(这就是您在 while found == true
位中所做的)。
或者你只问一次,看看是否找到,为此你需要删除 while found == true
部分。
我明白你的意思可能是 DirtyBit 所做的:
更好的版本是这个
usernames = ["u1", "u2", "u3"]
while True:
username = input("Enter username:")
if username in usernames:
print("Username found in index:", usernames.index(username))
break
else:
print("Sorry,username not found")
根据要求编辑:
usernames = ["u1", "u2", "u3"]
found = False
while found is False:
found = False
username = input("Enter username:")
for i in range(len(usernames)):
if usernames[i] == username:
print("Username found in index:", i)
found = True
break
if found is False:
print("Sorry,username not found")
你真的需要 while
块吗?
usernames=["u1","u2","u3"]
index = 0
found = False
username = input("Enter username:")
for i in range(len(usernames)):
if username == usernames[i]:
found = True
index = i
break
if found:
print("Username found in index:",index)
else:
print("Sorry,username not found")
我有以下代码,用于搜索用户名列表,并在第一次输出时简单地 return 输出 "Found in index i" 或 "Sorry username not found"。
usernames=["u1","u2","u3"]
found=False
while found==False:
username=input("Enter username:")
for i in range(len(usernames)):
if username==usernames[i]:
found=True
break
if found==True:
print("Username found in index:",i)
else:
print("Sorry,username not found")
如果用户名正确,当前代码似乎可以工作,但如果使用了错误数据,例如 23234,那么它会重复问题并且不会跳转到代码底部的 if 语句(这就是我想要的) .
有人可以更正此代码并说明解决此问题的最有效方法。这可能与布尔标志 'found' 有关,我不明白为什么它没有爆发并进入底部 if 语句。提前致谢
while found==False:
使其循环,直到 found
变为 True
。
因此,如果它没有找到您正在寻找的用户名,它会循环并再次询问您。
此外,如果您想查看列表中是否存在某个字符串,只需使用 list.index()
method:
username=input("Enter username:")
try:
i = usernames.index(username)
except ValueError:
print("Sorry,username not found")
else:
print("Username found in index:",i)
您不需要那些布尔标志、基于范围的循环或额外的 if 条件:
usernames=["u1","u2","u3"]
while True:
user = input("Enter username: ")
if user in usernames:
print("Username found at Index: {}".format(usernames.index(user)))
break
else:
print("Sorry, username not found. Try again")
编辑:
但是,如果您必须继续当前使用 for 循环的方法,请在外部 for 循环中放置一个 else 块,如果找到则中断:
usernames = ["u1","u2","u3"]
found = False
while found == False:
username = input("Enter username: ")
for i in range(len(usernames)):
if username == usernames[i]:
print("Username found at Index: {}".format(i))
break
else: # not and indentation error
print("Sorry, username not found. Try again")
编辑 2:(没有布尔标志)
usernames = ["u1","u2","u3"]
while True:
username = input("Enter username: ")
for i in range(len(usernames)):
if username == usernames[i]:
print("Username found at Index: {}".format(i))
break
else: # not and indentation error
print("Sorry, username not found. Try again")
OUTPUT(在所有情况下):
Enter username: 2334
Sorry, username not found. Try again
Enter username: u2
Username found at Index: 1
如果输入正确的用户名,您的代码将永远不会到达第二个,因为它被设计为仅到达第二个 if
。
您必须决定是继续询问用户名直到输入正确的用户名(这就是您在 while found == true
位中所做的)。
或者你只问一次,看看是否找到,为此你需要删除 while found == true
部分。
我明白你的意思可能是 DirtyBit 所做的:
更好的版本是这个
usernames = ["u1", "u2", "u3"]
while True:
username = input("Enter username:")
if username in usernames:
print("Username found in index:", usernames.index(username))
break
else:
print("Sorry,username not found")
根据要求编辑:
usernames = ["u1", "u2", "u3"]
found = False
while found is False:
found = False
username = input("Enter username:")
for i in range(len(usernames)):
if usernames[i] == username:
print("Username found in index:", i)
found = True
break
if found is False:
print("Sorry,username not found")
你真的需要 while
块吗?
usernames=["u1","u2","u3"]
index = 0
found = False
username = input("Enter username:")
for i in range(len(usernames)):
if username == usernames[i]:
found = True
index = i
break
if found:
print("Username found in index:",index)
else:
print("Sorry,username not found")