我正试图在文件中搜索一个数字。如果数字在文件中,它将显示该行。但是,如果不是,我希望它说找不到产品。我尝试了以下代码,但没有找到产品显示
def find_item():
product=input("Enter your product number here: ")
search=open("products.txt")
try:
for x in search:
if product in x:
print(x)
except:
print("product not found")
find_item()
Tags:
如果找不到产品,
try
下的代码不会产生任何异常。使用标志变量可以更轻松地完成此任务:相关问题 更多 >
编程相关推荐