Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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 Spyder不在多次运行中更新变量_Python_Spyder - Fatal编程技术网

Python Spyder不在多次运行中更新变量

Python Spyder不在多次运行中更新变量,python,spyder,Python,Spyder,我最近刚开始使用python,但对编码并不陌生。我一直在使用Spyder IDE练习一些不同的东西。我目前在战舰项目上遇到了麻烦 我第一次运行它时,它工作正常。但是在我运行了一次之后,大部分时间在我再次运行之后,它不会根据代码中给出的新值更新一些变量。我的意思是,它正在使用上一次运行的值,并且在我结束程序之前不会更新它们 通常,船舶位置和方位词不会更新。我还只是运行了确定这些变量的代码部分,并打印了它们,其他什么都没有。这似乎很好,但整个过程在整体运行时似乎在某种程度上把事情搞砸了 我的代码如下

我最近刚开始使用python,但对编码并不陌生。我一直在使用Spyder IDE练习一些不同的东西。我目前在战舰项目上遇到了麻烦

我第一次运行它时,它工作正常。但是在我运行了一次之后,大部分时间在我再次运行之后,它不会根据代码中给出的新值更新一些变量。我的意思是,它正在使用上一次运行的值,并且在我结束程序之前不会更新它们

通常,船舶位置和方位词不会更新。我还只是运行了确定这些变量的代码部分,并打印了它们,其他什么都没有。这似乎很好,但整个过程在整体运行时似乎在某种程度上把事情搞砸了

我的代码如下。很多评论都是为了帮助理解我可能复杂的过程。有什么想法吗

"""
Created on Thu Jan 12 16:20:01 2017

@author: zroland
"""
# need random for ship location and orientation
import random as rng

# make the board a list of lists
board = [['O' for x in range(10)] for y in range(10)]

# prints the board
def print_board(board):
    i = 0
    top = ''
    for k in range(len(board)):
        if k+1 < 10:
            top += '  ' + str(k+1) + '  '
        else:
            top += ' ' + str(k+1) + '  '
    print(top)
    for row in board:
        print(row, chr(97 + i))
        i += 1

# sets the length of the ship and calls the print_board function
ship_size = 2
print_board(board)

"""Sets ship orientation and determins the location of the ship based on that
(the ship cannot be placed at the end of the board if it is more than 1 unit
long)"""

ship_or = rng.randint(1, 2)
if(ship_or == 1):
    ship_or_w = 'Vertical'
    ship_r = chr(rng.randint(97, 97 + len(board) - ship_size))
    ship_c = rng.randint(1, len(board[0]))
else:
    ship_or_w = 'Horizontal'
    ship_r = chr(rng.randint(97, 97 + len(board) - 1))
    ship_c = rng.randint(1, len(board[0])-ship_size + 1)

# initializes the variable that keeps track of how many times you hit the ship
hit_count = 0

# shows location of ship, orientation, and size for development purposes
print(ship_r, ship_c, ship_or_w, ship_size)

"""Nested while loop. This is the game. The hit_count is checked to see if
the ship has been hit a number of times equal to its length. Need to edit this
to make sure you don't cheat and just hit the same spot multiple times to
win."""

while(hit_count != ship_size):

    """Initializes the guess location so the code will ask you for intput. This
    is to allow a the code to make sure you have a guess that is actually
    on the board. If it is not, it will continue to ask you for values until
    you do"""

    row1 = chr(97 + len(board))
    column = 1

    while((97 > ord(row1) or ord(row1) >= 97 + len(board))
            or (1 > column or column > len(board[0]))):
        """The first if statement is to make sure your column is good first.
        If it is not, there is no point in asking for a row. The column guess
        is initialized to be a correct value."""
        if(1 <= column <= len(board[0])):
            row1 = input("Enter your guess for the row: ")
            if(97 <= ord(row1) < 97 + len(board)):
                """if the row is a correct value, it creates/updates the
                variable we use to select the row in the board list."""

                row = ord(row1) - 97
            else:
                print('You did not enter a correct letter. Please try again')

        """Same as above. Checks for a correct row. No reason to ask for the
        column if the row is not right."""

        if(97 <= ord(row1) < 97 + len(board)):
            column = int(input("Enter your guess for the column: "))
            if(1 > column or column > len(board[0])):
                print("You did not enter a correct number. Please try again")

    # end of while loop that asks for/checks you guess

    """This is how the game checks if you hit or miss. The last conditional
    is the first one to think about. It determines the orientation of the
    ship, which in turn affects which guesses result in a hit.
    For the first if statement, if the orientation is 1 (vertical), it
    checks to see if your guess hit the first part of the ship (the actual
    location value determined at the start of the code) or any other
    space taken up by its length. If you did in fact hit the ship,
    it marks the board with an 'X' and tells you you hit it. It then
    increments the hit counter. The second if statement is for horizontal
    ships, and the third is in case you did not hit anything."""

    if((ord(ship_r) <= row <= ord(ship_r) + ship_size - 1) and column == ship_c
       and ship_or == 1):
        board[row][column-1] = 'X'
        print_board(board)
        print("You hit!")
        hit_count += 1
    elif(row1 == ship_r and (ship_c <= column <= ship_c + ship_size - 1)
         and ship_or == 2):
        board[row][column-1] = 'X'
        print_board(board)
        print("You hit!")
        hit_count += 1
    else:
        board[row][column-1] = '|'
        print_board(board)
        print('You missed :(')

# end of while loop that keeps asking you for guesses until you win

# prints hit count too to see if it is chekcking the value properly in the loop
print("You win!", hit_count)
“”“
创建于2017年1月12日星期四16:20:01
@作者:zroland
"""
#船舶位置和方向需要随机选择
将随机导入为rng
#给董事会列一份名单
电路板=[[O'表示范围(10)中的x]表示范围(10)中的y]
#打印电路板
def打印板(板):
i=0
顶部=“”
对于范围内的k(透镜(板)):
如果k+1<10:
top+=''+str(k+1)+''
其他:
top+=''+str(k+1)+''
打印(顶部)
对于板中的行:
打印(行,chr(97+i))
i+=1
#设置装运长度并调用print_board函数
船舶尺寸=2
印刷电路板
“”“设置船舶方向,并根据该方向确定船舶的位置。”
(如果船舶超过1个单位,则不能将其放置在板的末端。)
长“
船舶=rng.randint(1,2)
如果(ship_或==1):
ship_或_w=‘垂直’
船舶=chr(船舶尺寸)
ship_c=rng.randint(1,len(董事会[0]))
其他:
ship_或_w=‘水平’
船舶=chr(随机数(97,97+len(董事会)-1))
船舶尺寸c=rng.randint(1,长度(板[0])-船舶尺寸+1)
#初始化变量,该变量跟踪您撞船的次数
点击次数=0
#显示船舶的位置、方向和尺寸,以便于开发
打印(船号、船号c、船号或船号w、船号尺寸)
“”“嵌套while循环。这是游戏。检查命中率以查看是否
船被击中的次数与其长度相等。需要编辑此项吗
为了确保你不作弊,只需多次击中同一个点即可
赢
while(命中计数!=发货大小):
“”“初始化猜测位置,以便代码要求您输入。此
就是允许代码中的一个参数来确保你的猜测是真实的
在黑板上。如果不是,它将继续询问您的值,直到
你知道吗
行1=chr(97+len(董事会))
列=1
而((97>ord(第1行)或ord(第1行)>=97+len(董事会))
或(1>列或列>列(板[0]):
“”“第一个if语句是首先确保您的列是正确的。
如果不是,那么要求一行就没有意义了
已初始化为正确的值。”“”

如果(1)我还没有完全看完您的代码,但您应该知道,三重引号字符串不是块注释。它们是字符串文字,与任何其他字符串一样,除非它们是定义中的第一件事。请在此处阅读更多内容:您看到的输出如何让您认为值在p之后仍然存在程序结束?你是在一个更大的程序中多次运行这个程序,还是每次都直接运行它?我在spyder上运行过几次,它似乎运行得很好。它生成了3次水平,但每次的位置都不同。你能详细说明你到底想实现什么行为吗?我有我在代码中添加了一些非常短的暂停来修复类似的问题。我现在找不到这些代码,但我想我使用了一些100毫秒的暂停来让sypder赶上进度。(这可能是一个不同于您的spyder版本)Patrick Haugh,我来看看评论文章,谢谢。我看变量浏览器和程序是如何运行的。变量管理器直到代码运行后才更新,即使是输入值。有时它会在代码中间更新,但是只有在输入了其他东西之后,如果这是有意义的。它不是UPD。有一半的时间,当它给我船的坐标时,我猜,它说错过了。我相信这是因为这些值没有根据探测器更新。