Scrabble Project

on the scrabble project Scrabble Project/ Python3

I have a question because something just happened that completely baffled me. I know indentation is VERY important in Python but I’m trying to understand in this instance what exactly happened.

So, here is the code in question:

for player,words in player_to_words.items():
  player_points = 0
  for word in words:
    player_points += score_word(word)
  player_to_points[player] = player_points  
print(player_to_points)

This code above printed out the code correctly and fine… with all the players shown and the points they had.

This code below was incorrect as it only showed “Prof Reader” with 31 points. Why?! Why did that indentation do that?

for player,words in player_to_words.items():
  player_points = 0
  for word in words:
    player_points += score_word(word)
player_to_points[player] = player_points  
print(player_to_points)  

That line is outside of the outer loop, when it should perhaps remain in the loop as it is in the first example.

this may seem like a dumb question but how can you tell if it is in the outer loop or inner loop just by looking at the indentation??

Indentation defines a code block scoped to the signature line.

def foo(bar):
    for x in bar:
        print (x)

The top line is the signature line of the function. The second line denotes the code block of the function. It is the signature line of a for loop. The line that follows that is indented to denote the code block scoped to the loop.

A signature line (perhaps I’m the only person to call it this) is always terminated by a colon, and the line following is always indented.

for player, words in p_to_w.items():
    player_points = 0
    for word in words:
        player_points += score_word(word)
    player_to_points[player] = player_points  
    print(player_to_points)

You say that as if indentation was optional, and that there’s something else defining where the block starts and stops.

So maybe the better quesiton is: how would you tell without looking at indentation?

Thank you for the reply mtf that helps me understand a little better. Now, i just ran into another problem in the same issue with indentation , and i need to know why this happened also. So, on the coding challenge: Dictonaries Sum Values

i typed in this code:

def sum_values(my_dictionary):
  total = 0
  for value in my_dictionary.values():
    total += value
    return total

and was getting 5, 1 as answers, which is wrong, instead of 10,6. Now after removing the indentation on the return line as in the code below i received the proper answer 10, 6. Please help me understand this.

def sum_values(my_dictionary):
  total = 0
  for value in my_dictionary.values():
    total += value
  return total
  

Perhaps if you executed both in your head? Step through it. Watch where you go wrong, or at least identify what it is you don’t know.

If your confusion is coming from using a language with C-style syntax that uses braces, then perhaps translating the code to that other language helps you better read it. Note though that this other language probably indents exactly the same way, even if that language ignores whitespace.

In your first example, the return line is in the for loop code block, whereas the second example has it in the function code block, after the loop has completed iterating over the entire list of values.

If we write the return like in your first example, the loop only iterates once, then the return exits the function, prematurely.

The first level of indentation is the function code block. The second level of indentation is the for loop block.

Ohhhhhhhhh!! Well that helped me understand it better and whats exactly going on. I have another indentation question. Now this function the indentation went the “other” way as in the freqs[word] += 1 line underneath was indented less than the line of code above it. Why? is it because it would have been in an outer loop when it instead needs to be in the inner loop or am i fudging this all up??

def frequency_dictionary(words):
  freqs = {}
  for word in words:
    if word not in freqs:
    	freqs[word] = 0
    freqs[word] += 1
  return freqs

Everything looks good. The IF is checking for the key in the dictionary, if it does not exist then it is created and initialized to zero. The line following that always executes, but only after this check/initialization.

Will try this solution out

It does check out…

>>> a = "she sells seashells down by the seaside".split()
>>> frequency_dictionary(a)
{'seaside': 1, 'by': 1, 'down': 1, 'she': 1, 'seashells': 1, 'sells': 1, 'the': 1}
>>> 

This is my complete and correct solution for the project please feel free to refer

Heres the complete solution: