Why Write a Loop Inside of a Loop?


#1

Why I see it’s common to do a loop inside loop, instead of just one loop using the index? Is it superior in terms of performance and/or bug-fixing?

For example below is my code:
def scrabble_score(word):
  total = 0
  for i in word:
    total += score[i.lower()]
  print total

How can I access a dictionary key value inside of a loop?
#2

Hi @chipplayer31971,

There’s no loop inside of a loop there, that’s a loop inside of a function. The purpose of a function is to give you a piece of code you can call again and use at various points in the code.

Usually, when a loop is used inside a loop, it’s because that’s the simplest way to get the desired result, not because it’s faster.