Codecademy战舰!蟒蛇

时间:2014-12-08 22:23:10

标签: python

这是“战列舰!” Codecademy(Python)中涉及的问题我想提出一个条件,其中在用户输入重复或超出限制的行/列值的情况下,转数不会增加。我无法弄清楚这段代码中的问题:

from random import randint

board = []

for x in range(5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

print "Let's play Battleship!"
print_board(board)

def random_row(board):
    return randint(0, len(board) - 1)

def random_col(board):
    return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col

# Everything from here on should go in your for loop!
for turn in range(4):
    # Be sure to indent four spaces!
    print "Turn", turn + 1
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))

    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        break
    else:
        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
            print "Oops, that's not even in the ocean."
            print "Please try again."
#            turn -= 1                                        Why doesn't this work? The "Game Over" message does not get displayed at the end of the game if such a case arises (but the game still ends!), which means the value of turn does not reach 3. But still when 'turn + 1' is printed, the value gets incremented even if such a condition is encountered. 
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
            print "Please try again."
#            turn -= 1                                        Same as last message.
        else:
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"
        if turn == 3:
            print "Game Over"
        # Print (turn + 1) here!
        print_board(board)

2 个答案:

答案 0 :(得分:1)

for turn in range(4):循环不会像您认为的那样起作用。

它等同于:

_range = [0, 1, 2, 3]
_it = iter(_range)
 while True:
    try:
        turn = next(_it)
    except StopIteration:
        break
    # your code here

你不必理解那里的所有细节,但它们的关键点在于,每次循环时,它都会为turn分配一个新值,它直接来自迭代器,并且与turn的旧值无关。因此,您对turn的更改无关紧要。


那么,你如何解决它?

一种方法是将for循环替换为显式维护while的{​​{1}}循环,这样您就可以显式减少它 - 或者更好,只是不增加它。例如:

turn

另一种方法是在这个turn = 0 while turn < 4: # your code, slightly modified # do turn += 1 only when the turn is valid # don't do turn += 1 when the turn is invalid 循环中放置另一个循环,直到玩家进行有效转弯:

for

当我在这时,你的代码中还有另一个问题。

for turn in range(4): while True: # your code, slightly modified # do break only when the turn is valid # do nothing, or continue, when the turn is invalid 部分错误或不必要。当玩家不在时,游戏结束。你知道当你完成循环时玩家不在转弯状态。你不需要任何额外的测试。所以:

if turn == 3

答案 1 :(得分:1)

根据我的看法,您的代码中只有一个非常小的错误。最后一个if语句,其中包括你的#34; Game Over&#34;字符串,缩进不正确。向左移动一个块:

  else:
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"

if turn == 3:
  print "Game Over"
  # Print (turn + 1) here!
  print_board(board)

以下是该练习的完整代码:

from random import randint

board = []

for x in range(5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

print "Let's play Battleship!"
print_board(board)

def random_row(board):
    return randint(0, len(board) - 1)

def random_col(board):
    return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col

for turn in range(4):
    print "Turn", turn + 1

    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))

    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
    else:
        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
            print "Oops, that's not even in the ocean."
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
        else:
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"

        print_board(board)

    if turn == 3:
        print "Game Over"