我已经在商店类方法搜索中提到如果 for 循环结束并且仍然没有返回则返回 None 并且在 main 中我只写了如果它是 none 然后什么都不打印但它仍然打印“什么”我不认为有任何缩进正如在类似类型的问题中指出的那样
class book:
def __init__(self,_id,name,tech,price,auth):
self._id=_id
self.name=name
self.tech=tech
self.price=price
self.auth=auth
def _print(self):
print(self._id)
print(self.name)
print(self.tech)
print(self.price)
print(self.auth)
class store:
def __init__(self,bookdb,name='abc'):
self.bookdb=bookdb
self.name=name
def search(self,b_name,book_list):
for i in book_list:
if i.name==b_name:
return i._print()
else:
return None
def discount(self,tech,book_list):
amt=0
for i in book_list:
if i.tech==tech:
amt+=i.price
return amt*(0.9)
if __name__=="__main__":
t = int(input())
b_list=[]
bookdb=dict()
for i in range(t):
_id=int(input())
name=str(input())
tech=str(input())
price=int(input())
auth=str(input())
b_list.append(book(_id,name,tech,price,auth))
bookdb[i]=book(_id,name,tech,price,auth)
title=str(input())
tech=str(input())
store_i=store(bookdb)
if store_i.search(title,b_list)== None:
print('nothing')
else:
store_i.search(title,b_list)
if store_i.discount(tech,b_list)== 0:
print('0.0')
else:
print(store_i.discount(tech,b_list))
**Input**
3
1
TIC
CPP
300
Online
2
CR
JAVA
500
BSwamy
3
BR
JAVA
700
RAJA
TIC
JAVA
输出
1
TIC
CPP
300
Online
nothing
1080.0
需要输出
1
TIC
CPP
300
Online
1080.0
PS bookdb dic 在这里没有任何用处
class book中的_print()方法没有return语句,所以python会隐式返回None。然后在搜索函数中,当您返回 i._print() 时,它也会返回None,这就是您看到“无”输出的原因。
既然你要测试结果,你不妨让search()返回一个布尔值,这里有一个建议的编辑:
def search(self,b_name,book_list):
for i in book_list:
if i.name==b_name:
i._print()
return True
else:
return False
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句