使用布尔标志、逻辑和缩进错误中断python for循环
我有下面的代码来搜索用户名列表,并在第一次输出时简单地返回输出“在索引I中找到”或“对不起,找不到用户名”使用布尔标志、逻辑和缩进错误中断python for循环,python,list,loops,boolean,break,Python,List,Loops,Boolean,Break,我有下面的代码来搜索用户名列表,并在第一次输出时简单地返回输出“在索引I中找到”或“对不起,找不到用户名” 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 fo
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语句的底部。提前感谢
而found==False:
使其循环,直到found
变为True
。
因此,如果它没有找到您要查找的用户名,它会循环并再次询问您
另外,如果要查看列表中是否存在字符串,只需使用list.index()
:
您不需要这些布尔标志、基于范围的循环或额外的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:(不带布尔标志)
输出(在所有情况下):
您的代码永远不会到达第二个if,因为它被设计为仅在输入正确的用户名后才能到达第二个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")
把另一个块放在处理错误数据“缩进错误”上?这不回答你关于嵌套循环的问题,但是考虑一个更简单的方法来确定一个字符串是否在列表中:<代码>用户名中的用户名:< /代码>移除外部while循环……你根本不需要while循环,如果存在匹配项,只需将find设置为true即可。是的,当找到用户名时,在上面的代码中,它将变为true。(那个钻头起作用了)。不起作用的是,在另一种情况下(当找不到它时),我希望它跳转到下面的代码(if语句)并说“对不起,在列表中找不到用户名”,就像我说的那样,
while found==False:
语句使它这样循环。如果你不想让它循环,那就摆脱这个while循环吧!谢谢你-不确定它是否回答了这个问题。您能否更正代码并发布解决方案,使其符合问题的要求,即“如果用户名存在,则打印“索引中存在…”和“抱歉,没有用户名”。我不希望在用户名中使用“如果用户名存在”“因为我们需要使用for循环来演示眼睛,这很好,但正如前面提到的,我想使用for循环。我想用for循环解决这个问题。(这是为了教授算法)它不起作用-它重复“发现用户名或抱歉用户名”unnecessarily@MissComputing对不起,复制粘贴错误。现在试试看,那不行。NameError:name'found'没有为索引定义,enumerate中的候选项(用户名):if username==candidate:…
比在一个范围内迭代更惯用。if found:
,而不是if found==True:
。是的,有效!但这让我想知道,最有效的方法是不挂国旗。在for循环中,是否可以不使用found=false和if语句@是的,它可以。但效率几乎是一样的。我被难住了谢谢-这很有效,但我不完全理解。为什么“else”位于“for”的缩进级别,而不是原始的“if”。我显然不懂python缩进。我尝试了完全相同的东西,所以逻辑上是正确的,但我的另一个在同一行的是“如果”,它重复了blocks@MilesDavis不,那不是个错误。Python有for/else结构。在这里阅读@MissComputing可以这样想,如果else
块位于if
级别,那么如果用户名未放在第一个索引上,则为真。for循环还有一个else子句!我的不好,从未用于/else语句。很好-我不知道!我想这是python独有的。。。!
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")
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")