Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的战舰!python_Python - Fatal编程技术网

我的战舰!python

我的战舰!python,python,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) pri

这就是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)
来自随机导入randint
董事会=[]
对于范围(5)内的x:
董事会追加([“O”]*5)
def打印板(板):
对于板中的行:
打印“”连接(行)
打印“让我们玩战舰吧!”
印刷电路板
def随机_行(板):
返回randint(0,len(板)-1)
def随机_柱(板):
返回randint(0,len(板[0])-1)
船行=随机船行(板)
船列=随机列(船列)
打印发货行
打印船型
#从现在开始,一切都应该进入你的循环!
对于交车范围(4):
#一定要缩进四个空格!
打印“回合”,回合+1
猜测行=int(原始输入(“猜测行:”)
guess\u col=int(原始输入(“guess col:”)
如果guess\u row==ship\u row和guess\u col==ship\u col:
打印“祝贺你!你击沉了我的战舰!”
打破
其他:
如果(猜测行<0或猜测行>4)或(猜测列<0或猜测列>4):
打印“哦,那甚至不在海里。”
打印“请再试一次。”
#回合-=1为什么这不起作用?如果出现这种情况,“游戏结束”消息不会在游戏结束时显示(但游戏仍然结束!),这意味着回合值不会达到3。但当打印“turn+1”时,即使遇到这种情况,该值也会增加。
elif(董事会[猜测行][猜测列]=“X”):
打印“你已经猜到了。”
打印“请再试一次。”
#旋转-=1,与上一条消息相同。
其他:
打印“你错过了我的战舰!”
棋盘[猜一排][猜一列]=“X”
如果匝数=3:
打印“游戏结束”
#在这里打印(旋转+1)!
印刷电路板
A
对于上缴范围(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
循环,这样您就可以显式地减少它,或者更好地说,不增加它。例如:

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

当我在做的时候,你的代码中还有一个问题

如果转动==3,则该
部分要么错误,要么不必要。当玩家轮换时,游戏结束。当你完成这个循环时,你知道运动员的轮数不齐。你不需要任何额外的测试。因此:

for or while or whatever:
    # stuff    
print "Game Over"

据我所见,您的代码中只有一个非常小的错误。最后一个if语句(包括“Game Over”字符串)缩进错误。将其向左移动一个街区:

  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"
来自随机导入randint
董事会=[]
对于范围(5)内的x:
董事会追加([“O”]*5)
def打印板(板):
对于板中的行:
打印“”连接(行)
打印“让我们玩战舰吧!”
印刷电路板
def随机_行(板):
返回randint(0,len(板)-1)
def随机_柱(板):
返回randint(0,len(板[0])-1)
船行=随机船行(板)
船列=随机列(船列)
打印发货行
打印船型
对于交车范围(4):
打印“回合”,回合+1
猜测行=int(原始输入(“猜测行:”)
guess\u col=int(原始输入(“guess col:”)
如果guess\u row==ship\u row和guess\u col==ship\u col:
打印“祝贺你!你击沉了我的战舰!”
其他:
如果(猜测行<0或猜测行>4)或(猜测列<0或猜测列>4):
打印“哦,那甚至不在海里。”
elif(董事会[猜测行][猜测列]=“X”):
打印“你已经猜到了。”
其他:
打印“你错过了我的战舰!”
棋盘[猜一排][猜一列]=“X”
印刷电路板
如果匝数=3:
打印“游戏结束”

您希望我们阅读所有代码,运行并测试它吗?请阅读。把这整件事简单地说成是最基本的。例如,请参见。如果你给了我们,而不是你的整个计划,我敢打赌你的问题将以2票接近的票数获得+3票而不是-5票。你在打印“游戏结束:
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"