mardi 9 février 2021

why are both if and else statement getting executed {python}

i have mentioned in store class method search to return None if the for loop end and still no return and in main i have written only if its a none then print nothing but it still prints "nothing" i don't think there is any indentation as pointed out in questions of similar type

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

Output

1
TIC
CPP
300
Online
nothing
1080.0

output required

1
TIC
CPP
300
Online
1080.0

P.S. bookdb dic doesnt have any use here

Aucun commentaire:

Enregistrer un commentaire