I want to write a function that checks if the True boolena statement is within all_vals if there is a True value then the code will yes if not the code will output no, essentially creating an or statement. The code below does not work how would I be able to modify it so that it will get the Expected output below?
def vals(all_vals):
for x in all_vals:
if True in all_vals:
print('yes')
else:
print('no')
a = [True, True, True]
b = [True, False, True, True, False]
c = [False, False]
d = [True, False]
vals([a,b,c,d])
Expected Output:
yes
yes
no
yes
CodePudding user response:
Instead of: if True in all_vals: you need if True in x: like this:
def vals(all_vals):
for x in all_vals:
if True in x:
print('yes')
else:
print('no')
a = [True, True, True]
b = [True, False, True, True, False]
c = [False, False]
d = [True, False]
vals([a,b,c,d])
Also you can shorten your code durastically by instead using:
vals = lambda all_vals: print("\n".join(['yes' if True in i else 'no' for i in all_vals]))
a = [True, True, True]
b = [True, False, True, True, False]
c = [False, False]
d = [True, False]
vals([a,b,c,d])
Output:
yes
yes
no
yes
CodePudding user response:
def vals(all_vals):
for x in all_vals:
if True in x: # <--- Your typo here
print("yes")
else:
print("no")
a = [True, True, True]
b = [True, False, True, True, False]
c = [False, False]
d = [True, False]
vals([a, b, c, d])
