Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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_Variables_Random_Grid - Fatal编程技术网

如何随机化网格中变量的位置(python中)

如何随机化网格中变量的位置(python中),python,variables,random,grid,Python,Variables,Random,Grid,我一直在用python制作一个游戏,其中显示一个包含四个字母的2x2网格。 其中一个字母被替换为一个新字母。每次显示网格时,新字母必须是不同的字母,并且在网格上的位置必须不同。我创建了一个名为“newletter”的变量,但每次显示该变量时,我都无法将其在网格上的位置随机化。到目前为止,我的代码可以工作,但它不会更改变量的位置 “字母”是一个包含5个不同字母的文本文件 random.shuffle (letters) print (letters[0]+ " " +letters[1]) pr

我一直在用python制作一个游戏,其中显示一个包含四个字母的2x2网格。 其中一个字母被替换为一个新字母。每次显示网格时,新字母必须是不同的字母,并且在网格上的位置必须不同。我创建了一个名为“newletter”的变量,但每次显示该变量时,我都无法将其在网格上的位置随机化。到目前为止,我的代码可以工作,但它不会更改变量的位置

“字母”是一个包含5个不同字母的文本文件

random.shuffle (letters)

print (letters[0]+ " " +letters[1])
print (letters[2]+ " " +letters[3])

random.shuffle (letters)

replacedletter = letters[3]
newletter = letters[4]

print (letters[0]+ " " +letters[1])
print (letters[2]+ " " +newletter)

任何帮助都将不胜感激

这里有一个更简单的方法

# 4 letters, for sanity
grid = ['a','b','c','d']
# determine replacement letter
new_letter = 'e'

# get random position (4-value list has positions 0,1,2,3)
position = random.randint(0,3)

# set the position to a new value
grid[position] = new_letter

# one letter should have been set to 'e'
print grid

这里有一个更简单的方法

# 4 letters, for sanity
grid = ['a','b','c','d']
# determine replacement letter
new_letter = 'e'

# get random position (4-value list has positions 0,1,2,3)
position = random.randint(0,3)

# set the position to a new value
grid[position] = new_letter

# one letter should have been set to 'e'
print grid

这确实很有帮助,但字母需要以2x2的网格显示。我在随机化新词的位置时遇到了问题,因为它是网格格式的。这非常有用,但是字母需要以2x2的网格显示。我在随机化新词的位置时遇到困难,因为它是网格格式的。