Python 为什么我的数组编辑不能正常工作?

Python 为什么我的数组编辑不能正常工作?,python,Python,我是Python的初学者,正在制作一个棋盘游戏,玩家可以在其中输入他们想要左/右移动的位置等。但是有时它移动正确,有时不正确。我想这和我的网格阵列有关,但我不确定。。。如果有人能帮我,那就太好了,谢谢 choice=0 b=0 oldP=0 newP=0 player_location='X' x=8 y=0 xi=0 yi=0 up=0 down=0 left=0 right=0 new_board=[xi][yi] gold_coins=0 bandits=5 treasure_chests

我是Python的初学者,正在制作一个棋盘游戏,玩家可以在其中输入他们想要左/右移动的位置等。但是有时它移动正确,有时不正确。我想这和我的网格阵列有关,但我不确定。。。如果有人能帮我,那就太好了,谢谢

choice=0
b=0
oldP=0
newP=0
player_location='X'
x=8
y=0
xi=0
yi=0
up=0
down=0
left=0
right=0
new_board=[xi][yi]
gold_coins=0
bandits=5
treasure_chests=10
a=1
xi2=0
yi2=0

import random
def menu():
    print('If you would like to play the Treasure Hunt , press 1')
    choice=input('If not, press any key to exit')
    if choice=='1':
        print('Great! You have made the right choice :)')
    else:
        print('Goodbye.')
        quit()
menu()
def grid():
    new_board = [ ]

def board():
  new_board = [ ]
  top_row = [' 1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 ']

  new_board.append(top_row)

  for x in range(0,8):
    row = [' 0 ']*8
    new_board.append(row)
  return new_board

def print_board(b):
  row_letters = [' ','A','B','C','D','E','F','G','H']
  i = 0
  for row in b:
    print (row_letters[i],''.join(row))
    i=i+1
new_board = board()
xi=int(8)
yi=int(0)
new_board[xi][yi] = player_location
print_board(new_board)
while a==1:
    upordown=input('Would you like to move up or down? Enter \'u\' for up or \'d\' for down.').lower()
    upordown=upordown.lower()
    while not (upordown== 'u' or upordown== 'd'):
        print('Invalid input. Please try again')
        upordown= input().lower()
    while upordown=='u':
        try:
            up=int(input('How many spaces would you like to move up?'))
            b=0
        except:
            print('This is not a valid input, please enter a number.')
            b=1
        if b==0:
            break
    while upordown=='d':
        try:
            down=int(input('How many spaces would you like to move down?'))
            b=0
        except:
            print('This is not a valid input, please enter a number.')
            b=1
        if b==0:
            break
    leftorright=input('Would you like to move left or right Enter \'l\' for left or \'r\' for right.').lower()
    leftorright.lower()
    while not (leftorright== 'l' or leftorright== 'r'):
        print('Invalid input. Please try again')
        leftorright= input().lower()
    while leftorright=='l':
        try:
            left=int(input('How many spaces would you like to move left?'))
            b=0
        except:
            print('This is not a valid input, please enter a number.')
            b=1
        if b==0:
            break      
    while leftorright=='r':
        try:
            right=int(input('How many spaces would you like to move right?'))
            b=0
        except:
            print('This is not a valid input, please enter a number.')
            b=1
        if b==0:
            break
    print('Okay...')

    grid()
    while True:
        board()
        player_location=' X '
        if upordown=='d' and leftorright=='l':
            new_board[y-down][x-left] = player_location
            new_board = board()
            xi2=int(xi2-left)
            yi2=int(yi2-down)
            print ("The current x location is",xi)
            print ("The current y location is",yi)
            print ("The amount you chose to go down was",down)
            print ("The amount you chose to go right was",left)
            xi = int(xi+left)
            print("The new x location is",xi2)
            yi = int(yi+down)
            print("The new y location is",yi2)
            print(' ')
            while 0>xi2 or xi2>8 or 0>yi2 or yi2>8:
                print('Your move was illegal. Please enter a move that will position you within the grid')
                left=int(input('Please enter the number of moves you would like to move right'))
                down=int(input('Please enter the number of moves you would like to move up.'))
                xi=int(xi+left)
                yi=int(yi+down)
            new_board[xi][yi] = player_location
            print_board(new_board)
            break
        elif upordown=='d' and leftorright=='r':
            new_board = board()
            xi2=int(xi2+right)
            yi2=int(yi2-down)
            print ("The current x location is",xi)
            print ("The current y location is",yi)
            print ("The amount you chose to go down was",down)
            print ("The amount you chose to go right was",right)
            xi = int(xi+right)
            print("The new x location is",xi2)
            yi = int(yi+down)
            print("The new y location is",yi2)
            print(' ')
            while 0>xi2 or xi2>8 or 0>yi2 or yi2>8:
                print('Your move was illegal. Please enter a move that will position you within the grid')
                right=int(input('Please enter the number of moves you would like to move right'))
                down=int(input('Please enter the number of moves you would like to move down.'))
                xi=int(xi+right)
                yi=int(yi+down)
            new_board[xi][yi] = player_location
            print_board(new_board)
            break
        elif upordown=='u' and leftorright=='l':
            new_board = board()
            xi2=int(xi2+left)
            yi2=int(yi2-up)
            print ("The current x location is",xi)
            print ("The current y location is",yi)
            print ("The amount you chose to go up was",up)
            print ("The amount you chose to go left was",left)
            xi = int(xi+left)
            print("The new x location is",xi2)
            yi = int(yi-up)
            print("The new y location is",yi2)
            print(' ')
            while 0>xi2 or xi2>8 or 0>yi2 or yi2>8:
                print('Your move was illegal. Please enter a move that will position you within the grid')
                left=int(input('Please enter the number of moves you would like to move right'))
                up=int(input('Please enter the number of moves you would like to move up.'))
                xi=int(xi-left)
                yi=int(yi-up)
            new_board[xi][yi] = player_location
            print_board(new_board)
            break
        elif upordown=='u' and leftorright=='r':
            new_board = board()
            xi2=int(xi2+right)
            yi2=int(yi2+up)
            print ("The current x location is",xi)
            print ("The current y location is",yi)
            print ("The amount you chose to go up was",up)
            print ("The amount you chose to go right was",right)
            xi = int(xi-right)
            print("The new x location is",xi2)
            yi = int(yi+up)
            print("The new y location is",yi2)
            print(' ')
            while 0>xi2 or xi2>8 or 0>yi2 or yi2>8:
                print('Your move was illegal. Please enter a move that will position you within the grid')
                right=int(input('Please enter the number of moves you would like to move right'))
                up=int(input('Please enter the number of moves you would like to move up.'))
                xi=int(xi-right)
                yi=int(yi-up)
            new_board[xi][yi] = player_location
            print_board(new_board)
            break

我已经附上了我的所有代码,包括验证,这样用户就不会逃出网格,因为我不确定要包括哪些部分来明确我的问题和程序。

实践改进:

这里有一个关于调试的精彩介绍

我强烈推荐增量编程:编写几行代码,确保它们正常工作,直到现有代码正确无误后再继续。这样,当某些东西不起作用时,你知道它在你添加的最后几行。否则,您将得到200行代码,而不知道错误在哪里

节目更改

此代码不是为调试或维护而设计的;你应该在继续之前解决这些问题。最重要的是,您创建了两组执行一个任务(获取用户移动)的输入,然后创建了四个执行相同任务(移动用户)的代码块

相反,找到一些有用的措辞,让玩家在一行上输入移动。将其转换为移动坐标(例如[1,-2]表示“向上一个,向左两个”),然后使用一个代码块在网格中移动。测试一个代码块,而不是测试四个代码块;当您需要进行更改时,只需进行一次更改,而不是在旋转和反射下进行四次复制

结果 您使用的代码应该更像这样:

def get_move():
    advice  = "Please enter your move in two integers:\n" +
              "vertical, then horizontal.  Use positive numbers\n" +
              "for up and right, negative for down and left.\n"
    example = "For instance, the line \n\t2 -1\n" +
              "   is 2 spaces up, one space left."
    move = input(advice + example).split
    x_move = int(move[0])
    y_move = int(move[1])
    # Here, you check move legality; repeat until you get a legal move

    return x_move, y_move

while True:
    print_board()
    x_move, y_move = get_move()    # get_move contains the input interaction and data checking
    move(x_move, y_move)  # perform the move; update the board.
还请注意,您可能应该更新现有的板,而不是(显然)为每个移动创建一个新的板。在游戏结束之前,保持这个循环;不要在每一个动作上都爆发


这是否会促使您找到解决方案?

实践改进:

这里有一个关于调试的精彩介绍

我强烈推荐增量编程:编写几行代码,确保它们正常工作,直到现有代码正确无误后再继续。这样,当某些东西不起作用时,你知道它在你添加的最后几行。否则,您将得到200行代码,而不知道错误在哪里

节目更改

此代码不是为调试或维护而设计的;你应该在继续之前解决这些问题。最重要的是,您创建了两组执行一个任务(获取用户移动)的输入,然后创建了四个执行相同任务(移动用户)的代码块

相反,找到一些有用的措辞,让玩家在一行上输入移动。将其转换为移动坐标(例如[1,-2]表示“向上一个,向左两个”),然后使用一个代码块在网格中移动。测试一个代码块,而不是测试四个代码块;当您需要进行更改时,只需进行一次更改,而不是在旋转和反射下进行四次复制

结果 您使用的代码应该更像这样:

def get_move():
    advice  = "Please enter your move in two integers:\n" +
              "vertical, then horizontal.  Use positive numbers\n" +
              "for up and right, negative for down and left.\n"
    example = "For instance, the line \n\t2 -1\n" +
              "   is 2 spaces up, one space left."
    move = input(advice + example).split
    x_move = int(move[0])
    y_move = int(move[1])
    # Here, you check move legality; repeat until you get a legal move

    return x_move, y_move

while True:
    print_board()
    x_move, y_move = get_move()    # get_move contains the input interaction and data checking
    move(x_move, y_move)  # perform the move; update the board.
还请注意,您可能应该更新现有的板,而不是(显然)为每个移动创建一个新的板。在游戏结束之前,保持这个循环;不要在每一个动作上都爆发


这会促使您找到解决方案吗?

“我是Python的初学者…”-住手!你需要后退几步。像这样的项目不是初学者应该尝试做的。试着做一些类似于抽搐的动作,然后向上移动。@leaf绝对正确,有点冒险!然而,我认为这是一个网站上的初学者挑战,我真的希望完成它定义“初学者”。这是指初学者,比如刚刚下载Python,或者我已经做了几个月的Python初学者。现在,我看到的代码最大的问题是它非常冗长;yi2=int(yi2向下)。保持一致,使错误更加明显。例如,您总是可以向左和向下减去,向右和向上相加。通过这种方式,您将回顾您的逻辑,并使其在将来更容易理解。此外,试着看看你是否能找到一个模式,以确定它何时起作用,何时不起作用。它总是朝一个方向吗?它是否接近边缘?我强烈建议增量编程:编写几行代码,确保它们正常工作,在现有代码正确之前不要继续。这样,当某些东西不起作用时,你知道它在你添加的最后几行。否则,您将得到200行代码,而不知道错误在哪里。“我是Python的初学者…”-停止!你需要后退几步。像这样的项目不是初学者应该尝试做的。试着做一些类似于抽搐的动作,然后向上移动。@leaf绝对正确,有点冒险!然而,我认为这是一个网站上的初学者挑战,我真的希望完成它定义“初学者”。这是指初学者,比如刚刚下载Python,或者我已经做了几个月的Python初学者。现在,我看到的代码最大的问题是它非常冗长;yi2=int(yi2向下)。保持一致,使错误更加明显。例如,您总是可以向左和向下减去,向右和向上相加。通过这种方式,您将回顾您的逻辑,并使其在将来更容易理解。此外,试着看看你是否能找到一个模式,以确定它何时起作用,何时不起作用。它总是朝一个方向吗?它是否接近边缘?我强烈建议增量编程:编写几行代码,确保它们正常工作,在现有代码正确之前不要继续。这样,当某些东西不起作用时,你知道它在你添加的最后几行。否则,你