Python 在中使用if…then…else检查对象是否已存在
在创建新的生物块中,我想检查生物是否已经存在。我在其中添加了if…then…else结构,但当我运行程序并尝试添加新的生物(尚未存在的生物)时,输出为: 为你的新生物输入一个名字:狗新生物已经创建。 该生物名为:狗,该生物已存在 它会创建新的生物,但也会显示信息“该生物已经存在”。我怎样才能消除这个信息,使它只在生物已经存在的情况下出现Python 在中使用if…then…else检查对象是否已存在,python,Python,在创建新的生物块中,我想检查生物是否已经存在。我在其中添加了if…then…else结构,但当我运行程序并尝试添加新的生物(尚未存在的生物)时,输出为: 为你的新生物输入一个名字:狗新生物已经创建。 该生物名为:狗,该生物已存在 它会创建新的生物,但也会显示信息“该生物已经存在”。我怎样才能消除这个信息,使它只在生物已经存在的情况下出现 class Critter(object): noc = [] # I have omitted ther rest of the cod
class Critter(object):
noc = []
# I have omitted ther rest of the code of the class for simplicity.
# main
# Create a new critter/object using the class Critter and store it in the list named noc
new_crit = input("Enter a name for your new critter: ")
for i in noc:
if i.name != new_crit: # name is a parameter used in the class Critter
Critter.noc.append(Critter(new_crit))
print("New critter has been created. the critter is named: ", new_crit)
else:
print("That critter already exists")
如果Critter.noc中的i.name出现打印错误,则使用
而不是,否则Critter.noc.append(Critter(new_crit))
而不是而不是用于Critter.noc中的i.name。noc:
,如果Critter.noc中的i.name出现打印错误,则使用而不是Critter.noc.append(Critter(new_crit))
您正在noc
中测试每一个已经存在的生物,如果您有多个生物并且已经使用了所选的名称,您最终会添加一个新的生物并抱怨名称已经存在
将两个测试解耦;首先查看名称是否已经存在,并且仅在循环完成后添加新生物:
new_crit = input("Enter a name for your new critter: ")
for i in noc:
if i.name == new_crit: # name is a parameter used in the class Critter
print("That critter already exists")
return
# we can only get here if no such name was found, otherwise the function
# would have exited at the `return`
Critter.noc.append(Critter(new_crit))
print("New critter has been created. the critter is named: ", new_crit)
如果这不在函数内,您可以使用break
和for…else
(因此将else
块附加到for
语句,而不是If
语句):
只有当for
循环在没有中断的情况下完成所有迭代时,才会执行for
循环的else
块。由于break
仅在找到匹配的名称时执行,因此您可以安全地假设没有此类名称,并在else
块中创建一个新的Critter()
。您正在测试noc
中的每个现有生物,如果您有多个生物并且已经使用了拾取的名称,你最终既添加了一个新的生物,又抱怨这个名字已经存在
将两个测试解耦;首先查看名称是否已经存在,并且仅在循环完成后添加新生物:
new_crit = input("Enter a name for your new critter: ")
for i in noc:
if i.name == new_crit: # name is a parameter used in the class Critter
print("That critter already exists")
return
# we can only get here if no such name was found, otherwise the function
# would have exited at the `return`
Critter.noc.append(Critter(new_crit))
print("New critter has been created. the critter is named: ", new_crit)
如果这不在函数内,您可以使用break
和for…else
(因此将else
块附加到for
语句,而不是If
语句):
只有当for
循环在没有中断的情况下完成所有迭代时,才会执行for
循环的else
块。由于break
仅在找到匹配名称时执行,因此您可以放心地假设没有这样的名称,并在else
块中创建一个新的Critter()
。请将其缩减为一个。@jornsharpe我按照您的建议缩短了代码。现在请看一看,请把它缩减为一个。@jornsharpe我按照你的建议缩短了代码。现在请看一看,这不起作用,因为Critter.noc
包含Critter
实例,而不是名称。这不起作用,因为“for i”从list@MartijnPieters是的,你是对的。哎呀,我希望写一些对初学者来说很容易的代码,尽管这需要大量的资源,比如if Critter(new_crit)在Critter.noc:print(“该生物已经存在”)中,else:Critter.noc.append(Critter(new\u crit))print(“新生物已经创建。该生物的名称为:”,new\u crit)
,但如果不重载相等运算符,这是不可能的,因为它不是初学者的东西def\uu eq\uu(self,other):在Critter类中返回self.name==other.name
。这不起作用,因为Critter.noc
包含Critter
实例,而不是名称。这不起作用,因为“for i”从list@MartijnPieters是的,你是对的。哎呀,我想写一些对初学者来说很容易的代码,虽然资源密集,如Critter.noc:print(“该生物已存在”)中的if Critter(new_crit)中的中,否则:Critter.noc.append(Critter(new_crit))print(“新生物已创建。该生物名为:”,new_crit)
,但如果不重载相等运算符,则不可能,这不是初学者的东西def\uu eq\uu(self,other):在Critter类中返回self.name==other.name
。非常感谢。这两个例子都很好!非常感谢你。这两个例子都很好!