#๐Ÿ”’ for val in list

62 messages ยท Page 1 of 1 (latest)

brave aspen
#

Here's my code:

mylist = [1, 1, 0, 0, 0, 0, 0, 0, 0]
    
def myfunction(mylist):
    for val in mylist:
        if val != 0:
            return False
        else:
            return True
                
print(myfunction(mylist))

What return I'm expecting: True
What it gives me: False

My intentions: I want to check if there are any 0 in my list.
If there is atleast one 0 in my list, return True.

light stratusBOT
#

@brave aspen

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

brittle wadi
brave aspen
brittle wadi
#

yes, it's the outcome of calling the function

brave aspen
#

Exactly

brittle wadi
#

return is also the end of the function

#

once you reach a return line in a function, the function does not proceed as it has already found its outcome

brave aspen
#

Yes, I want to end the loop immediately if it checks a 0 in the list.

#

It saves some time

#

instead of going through a long list

brittle wadi
#

yes, but you also have your else: return True inside the loop

#

so right now you're returning True if the first number is not a zero

#

so it should return True if there is at least one zero?

#

Forget about code for a second. Imagine I had a bag of 1s and 0s

#

I ask you if there's any 0s in the bag

#

how would you check?

brave aspen
#

and if there's the first 0 I see, I close the bag

#

I ignore all the 1s I get

pale swallow
#

Except as it stands your function immediately returns False when encountering something a 1 it should ignore

brave aspen
#

Damn it

#

I see it, yet what alternative is there?

#

ok I could do a sum check

#

but... if I have a list of 100 items that's gonna take a while

brittle wadi
pale swallow
brave aspen
brittle wadi
brave aspen
#

I could do a brute force check

brittle wadi
#

I'm asking you about a bag of zeroes and ones

#

When will I know that there's no zeroes in the bag?

brave aspen
#

when everything's out

pale swallow
brittle wadi
#

yes exactly

brave aspen
#

so, brute force it is

brittle wadi
#

what part of your code is looking at everything in the bag?

brave aspen
#

none, since I got return ending the process prematurely

brittle wadi
#

you only need to end the process early if you find a zero

#

if you go through the whole process without finding a zero, then you can return False

#

there's no "brute force" about this as there's only a single list and single iteration

brave aspen
#

give me some time to change my code

#
mylist = [1, 1, 0, 0, 0, 0, 0, 0, 0]
    
def myfunction(mylist):
    for val in mylist:
        if val == 0:
            return True
        else:
            return False
                
print(myfunction(mylist))
#

is that it?

brittle wadi
#

again, you should not be returning both options within the loop

#

you can only safely return False after you've looked at every single number in the list

#

what part of your code is after checking each number?

brave aspen
#

wdym after checking each number?

#

oh nvm

#

I gotta check every item, is that what you mean?

brittle wadi
#

that's already what your for loop is doing

#

do you understand what a for loop does?

brave aspen
#

yeah,

brittle wadi
#

so, if your for loop finishes without returning True, that means there were no zeroes found

#

if there was a zero, the loop would stop early due to the return

brave aspen
#

in my case, it check the maximum amount of times, as long as the list is going

brave aspen
#

๐Ÿ‘ฉโ€๐Ÿฆฒ

brittle wadi
light stratusBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.