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!
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
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.
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:
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
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