How should my code be indented inside of the new for loop?

Question

How should my code be indented inside of the new for loop?

Answer

To ensure that we keep all of the same functionality per turn, just with the added ability to now take 4 turns before the game ends, we need to make sure we put everything we had before inside of the for loop we just created. The result should look like this (in pseudo code, to avoid giving the answer):

for turn in range(4):
  get guess_row
  get guess_col

  if guess is correct:
    print “Congrats! You sunk my ship.”
  else, not correct:
    if guess is invalid:
      print “Not in the ocean”
    elif already guessed:
      print “Already guessed!”
    else, just missed:
      print “Missed!”
      update element in board to be “X”
    print current turn
    print the board

Be sure to print the current turn + 1 like above as well!

2 Likes

Why do we have to indent all of the code twice? Wouldn’t once do?

3 Likes

Also, I used the str() tool to convert the ‘turn + 1’. Is this okay?

you can use [tab]key to indent properly

3 Likes

Hi, for the moment i only know one way to indent a part of a code : i indent every line with the [tab]key but … it is long … Is there a way to indent easily a all part of line once ? For exemple by selecting the part of code you want to indent and apply something … ?
Eric

1 Like

Highlight + tab to indent; highlight + Shift-tab to unindent

9 Likes

Thanks for your Help

1 Like

How does turn + 1 add one to “Turn”, which is a string and separate entity from turn? how does it know to add the strings 1-4 to the “Turn” string??

1 Like

In the expression for turn in range(4), turn is a variable. In particular, it is an iteration variable: it takes in sequence as its values, the integers 0 through 3 which are returned by the range() function.

That means that turn is an int, not a string.

2 Likes

I think I typed poorly. I realize that turn isn’t a string, I was just wondering how the variable turn is able to add a “1-4” to the “Turn” string. I know that it is keeping track of turns, but how it translates this to strings “1-4”, which are appended to “Turn”, is confusing me.

Well, “Turn” is a string, whereas turn is a variable, which has the value of int.

There are several ways to do it. Here are two:

  1. Take advantage of the fact that print, when used with a comma-separated sequence (which, technically is called a tuple), doesn’t care about mixing types, it will print a string representation of each object, separated by spaces.
for turn in range(2):
  print "Turn", turn + 1
# Output:
Turn 1
Turn 2
  1. Or with print, use strings, along with the + (concatenation) operator. You’ll need to cast turn, the variable, as a string:
for turn in range(2):
  print "Turn" + str(turn + 1)  
# Output:
Turn 1
Turn 2
3 Likes

I see, so it’s just Python being clever. Thank you.

It was possibly the instructor who chose to use the same name for a variable and a string being clever. Python is never clever, just blindly obedient. :slightly_smiling_face:

1 Like

Unfortunately, to successfully pass this step you’re not allowed to use str().

Ah! Well, actually, the Pythonic way would be to use string formatting:

for turn in range(2):
  print "Turn %d" %turn   # The use of d in the placeholder tells print to expect an int.

Output (Python 2):

Turn 0
Turn 1
1 Like

Thanks!! It is so helpful!

Code says
Everything from here on should go in your for loop!
Be sure to indent four spaces!

Error message says
Your indentation looks a bit off. Did you remember to indent two spaces for each indentation level?

Confusion at its best…

3 Likes

I do not understand what is going on here?

I disagree with this statement?