pylearner
pylearner

Reputation: 1460

am trying to reduce the score when a condition satisfies

Am trying to reduce a score when the condition satisifies. But failing to do so.

data = ['A','B']
Score = 10
words = [ 'C', 'D']

for i in data:
     if i in words:
          do nothing
     else:
         reduce score by 2

Here, when both A and B are not there in words, I want my score to reduce only once, but not twice.

Expected output : 8

code :

index = []
for i in data:
     if i in words:
          do nothing
     else:
         index.append(something)

if len(index) > 1:
       reduce score by 2

This is what I have written, but is there a way to make this even less complicated ??

Upvotes: 1

Views: 28

Answers (1)

Sowjanya R Bhat
Sowjanya R Bhat

Reputation: 1168

data = ['A','B']
score = 10
words = [ 'C', 'D']

data_not_found_list = [False for dt in data if dt not in words]

if not any(data_not_found_list):
    score -= 2

print(score)

Output : 8

I have made use of the any() method here . You can read how it works to get an idea - https://realpython.com/any-python/

Upvotes: 2

Related Questions