简体   繁体   中英

How can I get it to print something when the checker turns out false for every item in a list?

I'd like to add a print("something") if the checker turns out to be false for every item in the a_list. I'm VERY grateful for any help I can get!

def checker(lst, lstA):
    for i in range(4):
        if function(lst[i],lstA) != lst[i][1]: #testing FALSEHOOD
            return False;
    return True;


def main(???):
    for H in range(0,len(a_list)):
        if a_list[H] > lst[3][0]:
            continue

        lstA = [a_list[H]]

        if not checker(lst,lstA):
            continue

        lstA.append(input('some input from the user'))
        other_function(lstA)

        if lstA[1]== 40:
            print ('something something')
            return #break out of EVERY loop

Your question is a little vauge, so I am not 100% positive I am actually answering the question you were asking. Also, I am guessing the code was supposed to be indented in the following way:

def main(???):
    for H in range(0,len(a_list)):
        if a_list[H] > lst[3][0]:
            continue

        lstA = [number_list[i]]

        if not checker(lst,lstA):
            continue

        lstA.append(input('some input from the user'))
        other_function(lstA)

        if lstA[1]== 40:
            print ('something something')
            return #break out of EVERY loop

Also, you use the variable i , which does not seem to be defined anywhere.

You could add a variable outside of the for loop keeping track of if the check has been false for all values so far. In the beginning this should be True:

...
false_for_all = True
for H in range(0,len(a_list)):
...

Then, if the check succeeds you set it to False.

...
if not checker(lst,lstA):
    continue
false_for_all = True
...

Now you can use the false_for_all variable to check if the check failed for all values.

I don't understand where you want to add print("something") . If you want to add the statement in the checker function, you may add an indicator to store how many times the check fails:

def checker(lst, lstA):
    errors = 0
    for i in range(4):
        if function(lst[i],lstA) != lst[i][1]: #testing FALSEHOOD
            errors += 1
    if not errors:
        return True
    elif errors == 4:
        # print something here

    return False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM