0

Assuming played_chicket and winning_chicket are 2 random lists wiht 4 elements each, what does this loop returns? I can't understand the logic behind it. Won't it return True always because the ''return True'' statement is outside the loop? I know it will return false if any of the elements are different but i can't see why. Can someone please, explain it to me?

def check_ticket(played_ticket, winning_ticket):
    # Check all elements in the played ticket. If any are not in the 
    #   winning ticket, return False.
    for element in played_ticket:
        if element not in winning_ticket:
            return False
     
    # We must have a winning ticket!
    return True
  • 1
    There is no `return True` in your code – tkausl Mar 24 '22 at 00:38
  • 1
    Hey Adriano, could you show us the entire code and provide an input example for `played_ticket, winning_ticket` please? – Freddy Mcloughlan Mar 24 '22 at 00:39
  • hey guys, the return True statement was missing, here is the right version of the code – adriano guimaraes Mar 24 '22 at 00:54
  • Right now (which is to say, as of [revision 2](https://stackoverflow.com/revisions/71595707/2) of the question), that `return True` is _not in the function at all_ -- it would need to be indented more to be part of the function – Charles Duffy Mar 24 '22 at 00:55
  • Anyhow, remember that whenever a `return` statement is reached, _the entire function ends_. It's not "last return wins", it's _first_ return wins, because after a `return` is reached, nothing else in that instance of the function has any chance to run. So when a `return False` is reached, the function is done; it can't exit the loop and then reach `return True`. – Charles Duffy Mar 24 '22 at 00:57
  • 2
    You are basically just reimplementing `all`: `return all(x in played_ticket for x in return_ticket)`. If you can make `winning_ticket` a set, then `return set(winning_ticket).issuperset(played_ticket)`. – chepner Mar 24 '22 at 01:05
  • @chepner you can only make it a `set` only if all the elements are unique – Freddy Mcloughlan Mar 24 '22 at 01:19
  • @FreddyMcloughlan um, what? You can make a set out of any iterable of hashable objects, and you can certainly use them here – juanpa.arrivillaga Mar 24 '22 at 01:26
  • But wouldn't it be a logical error if you compare `[1,2,2]` to `[1,2]` which are both different tickets? as the set of both is the same. I was just adding to chepner's comment as he said "**If** you can make `winning_ticket` a set", and we don't know the conditions of those tickets. – Freddy Mcloughlan Mar 24 '22 at 01:30

2 Answers2

1

This is most likely because the first element in played_ticket is not in winning_ticket

Consider played_ticket = [0,1,2] and winning_ticket = [1,2]

Your first iteration will look like this:

    for element in played_ticket:
        # element = 0
        if element not in winning_ticket:
            # 0 is not in winning_ticket, so it will return false immediately
            return False

Your function as is will always return False or nothing (becomes None). You will need to edit the function to have a fallback return True:

def check_ticket(played_ticket, winning_ticket):
    # Check all elements in the played ticket. If any are not in the
    #   winning ticket, return False.
    for element in played_ticket:
        if element not in winning_ticket:
            return False
    # Here
    return True

So if every element is in the other list, it will return True


See this discussion if you would like to simplify your function

Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29
0

by turning winning ticket into a list you could invert the process checking if the element is True, return False if nothing is True:

def check_ticket(played_ticket, winning_ticket):
# Check all elements in the played ticket. If any are not in the 
#   winning ticket, return False.
    if any(element in winning_ticket for element in played_ticket):
        return True
    return False
structure
  • 35
  • 7