My problem is that I want to write a function newlist
, that takes in a list
of numbers as the parameter. It then returns a new list based on the parameter. The new list contains the same values of the original list up until it reaches the number 7.
my code that works:
def newlist(mylst):
newlst = []
idx = 0
while (idx < len(mylst)) and (mylst[idx] != 7):
newlst.append(mylst[idx])
idx += 1
return newlst
now when I switch the sides of the and
statement like this:
while (mylst[idx] != 7) and (idx < len(mylst)):
it tells me that this is wrong and when passing a list as parameter, the index is
out of range. My question is why does it make a difference with what side of the and
statement I start? I thought that the boolean is only True if both statements are True, regardless of order?