Why Write a Loop Inside of a Loop?

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

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.

1 Like

it’s weird but more weird is that this
word = raw_input("enter word: ")

def scrabble_score(word):
word = word.lower()
total = 0
for n in word:
total = total + score[n]
return total

print scrabble_score(word)

also works