Python loops and syntax

Hello!

I’ve been spending a lot of time on Python loops challenges mainly because I’ve failed the syntax so many times…

Yesterday I failed one challenge time after time because my return wasn’t indented correctly:

def exponents(bases, powers):
  if len(bases) > 0:
    new_list = []
    for base in bases:
      for power in powers:
        add_list = base ** power
        new_list.append(add_list)
      return new_list

exponents([2, 3, 4], [1, 2, 3]) returns [2, 4, 8]

but if I were to intend the return new_list correctly i.e. to make it appear under the first for loop:

def exponents(bases, powers):
  if len(bases) > 0:
    new_list = []
    for base in bases:
      for power in powers:
        add_list = base ** power
        new_list.append(add_list)
    return new_list

It would yield the desired answer.

I’ve searched for an explanation to this and found it. However, I still need an answer to the following question:

Today I failed another challenge because I didn’t know that it is best to define an empty list before for loop and not in the loop. Why is that I still don’t understand.

Here’s the desired code:

def over_nine_thousand(lst):
  sumlist = 0
  if len(lst) == 0:
    return 0
  if len(lst) > 0:
    for number in lst:  
      sumlist = sumlist + number
      if (sumlist > 9000):
        break
    return sumlist

Here’s what made me fail multiple times:

def over_nine_thousand(lst): 
  if len(lst) == 0:
    return 0
  if len(lst) > 0:
    for number in lst: 
      sumlist = 0
      sumlist = sumlist + number
      if (sumlist > 9000):
        break
    return sumlist

over_nine_thousand([8000, 900, 120, 5000]) returns 5000

As you can see, my sumlist = 0 is situated in a for loop. What fails in the above code? Why shouldn’t I create an empty list in a for loop but instead do it before the loop?

I think that more light should be shed on these things because they seem like unimportant but they turn the rest of the code into unusable garbage…

Thanks in advance to whoever will give and explanation to my last question!

Also, Merry Christmas and Happy New Year! :partying_face:

Did you think you put it somewhere else or did you put the return in some arbitrary location? You have to make a decision about when to return, right? Put it there, and not anywhere else.

Isn’t that the same thing? You have to make a decision about when to create your empty list.

If you rearrange the order of instructions then you’ll get a different outcome. That’s not surprising, is it?

These are different things:

turn left
turn left
turn left
turn left
walk forwards

turn left
turn left
walk forwards
turn left
turn left

The first one will have made you walk forwards once in the direction you were originally facing.

The second one will have made you walk in the opposite direction.

If you do something inside a loop it’s going to be repeated. If you do something outside a loop it will happen once.

Maybe you should execute your code manually to see what you end up doing.

1 Like