我一直在使用这个,但它似乎不适用于多个条目每次我做搜索后添加第二个条目,如果我试图搜索第一个条目,它会出现第二个。什么是修复
for i in range(len(gclients)):
record = gclients[i]
if record["Name"].lower() == search1:
if record["Surname"].lower() == search2:
recordfoundc = True
for k,v in record.iteritems():
resname = record["Name"]
resSurname = record["Surname"]
resnum = record["Phone Number"]
resjob = record["Job"]
resaddress = record["Address"]
resemID = record["Employee ID"]
if recordfoundc:
print"You have just found",resname,resSurname,resnum,resjob, resaddress, resemID
recordfoundc = False
else:
print "Client not found"
与代码相关:
在
if loop
(在recordfoundc = True
之后)内移动for k,v in record.iteritems():
for循环代码,因为当找到Employee时,只有从records
获取Employee详细信息。你知道吗不需要
for k,v in record.iteritems():
语句因为我们直接从记录中访问键和值,并且在代码中不使用变量k
和v
。你知道吗同时使用break语句。你知道吗
代码看起来像-:
Python允许用
and
和or
关键字在if循环中编写多个条件。你知道吗演示:
中断语句
当满足任何条件时,使用break语句退出any while。你知道吗
在我们的例子中,当员工的名字和姓氏在记录中匹配时,则不需要签入其他记录项。你知道吗
演示:在
3
中i
的值时中断for loop
。你知道吗如何从字典中获得价值。你知道吗
演示:
打印输出发生在for循环完成之后,因此结果变量只是您最后编写的变量(并且这些变量只需要在记录匹配的情况下写入,如注释中所指出的)。您需要在for中打印,或者将结果添加到列表中以便以后打印。和/或如建议的那样,如果您只想要第一个匹配,则从循环中断。你知道吗
相关问题 更多 >
编程相关推荐