145k views
3 votes
#Write a function called one_dimensional_booleans.

#one_dimensional_booleans should have two parameters:
#a list of booleans called bool_list and a boolean called
#use_and. You may assume that bool_list will be a list
#where every value is a boolean (True or False).
#
#The function should perform as follows:
#
# - If use_and is True, the function should return True if
# every item in the list is True (simulating the and
# operator).
# - If use_and is False, the function should return True if
# any item in the list is True (simulating the or
# operator).
#Write your function here!

User Piotrp
by
5.3k points

1 Answer

3 votes

Answer:

def one_dimensional_booleans(bool_list, use_and):

is_all_true = True

is_one_true = False

if use_and:

for b in bool_list:

if b == False:

is_all_true = False

break

if is_all_true:

return True

else:

return False

else:

for b in bool_list:

if b == True:

is_one_true = True

break

if is_one_true:

return True

else:

return False

Step-by-step explanation:

Create a function named one_dimensional_booleans that takes two parameters, bool_list and use_and

Inside the function:

Set is_all_true as True and is_one_true as False. These will be used to check the list

If use_and is True, check each item in the bool_list. If one item is False, set the is_all_true as False and stop the loop. This implies that the list contains a False value. Then check the is_all_true. If it is True, return True. Otherwise, return False.

If use_and is False, check each item in the bool_list. If one item is True, set the is_one_true as True and stop the loop. This implies that the list contains a True value. Then check the is_one_true. If it is True, return True. Otherwise, return False.

User Keithius
by
5.1k points