Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/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 2.7.4上的单词搜索_Python_Python 2.7 - Fatal编程技术网

Python 2.7.4上的单词搜索

Python 2.7.4上的单词搜索,python,python-2.7,Python,Python 2.7,我们正在尝试为一个学校项目制作一个游戏,并选择了单词搜索。我们研究并发现了一段代码,它可以帮助我们制作网格,并将我们的单词放入网格中。但我们现在面临的问题是网格没有正确对齐。(即,第一行的第三个字母不在第二行第三个字母的正上方。)。这使得很难找到单词。但不幸的是,我们没有研究如何使用“随机选择”,似乎无法理解如何正确对齐字母。所有的想法和意见都很感激 import string import random print '''Welcome to WORD SEARCH. This game ha

我们正在尝试为一个学校项目制作一个游戏,并选择了单词搜索。我们研究并发现了一段代码,它可以帮助我们制作网格,并将我们的单词放入网格中。但我们现在面临的问题是网格没有正确对齐。(即,第一行的第三个字母不在第二行第三个字母的正上方。)。这使得很难找到单词。但不幸的是,我们没有研究如何使用“随机选择”,似乎无法理解如何正确对齐字母。所有的想法和意见都很感激

import string
import random
print '''Welcome to WORD SEARCH. This game has five stages. You will have a crossword in each stage with one element hidden in each puzzle.
However, do remember that the game is over once you make a mistake. So, think carefully and play.
Good luck!!!!'''

width = 12
height = 12

def put_word(word,grid):
    word = random.choice([word,word[::-1]])
    d = random.choice([[1,0],[0,1],[1,1]])

    xsize = width  if d[0] == 0 else width  - len(word)
    ysize = height if d[1] == 0 else height - len(word)

    x = random.randrange(0,xsize)
    y = random.randrange(0,ysize)
    for i in range(0,len(word)):
        grid[y + d[1]*i][x + d[0]*i] = word[i]
    return grid

谢谢你

对我来说似乎很好。。。也许您应该在终端/打印网格的任何地方使用单间距字体

grid = [['_' for _ in range(width)] for _ in range(height)]

def print_grid():
    global grid
    for row in grid:
        print " ".join(row)

put_word("hello", grid)
put_word("world", grid)

print_grid()
不过,您可能需要修复算法,因为单词重叠,您可以看到我添加了
“hello”
,但是
“herlo”
却在那里

样本输出

_ _ _ h _ _ _ _ _ _ _ _
_ _ _ _ e _ _ _ _ _ _ _
_ _ _ d l r o w _ _ _ _
_ _ _ _ _ _ l _ _ _ _ _
_ _ _ _ _ _ _ o _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _

对我来说似乎很好。。。也许您应该在终端/打印网格的任何地方使用单间距字体

grid = [['_' for _ in range(width)] for _ in range(height)]

def print_grid():
    global grid
    for row in grid:
        print " ".join(row)

put_word("hello", grid)
put_word("world", grid)

print_grid()
不过,您可能需要修复算法,因为单词重叠,您可以看到我添加了
“hello”
,但是
“herlo”
却在那里

样本输出

_ _ _ h _ _ _ _ _ _ _ _
_ _ _ _ e _ _ _ _ _ _ _
_ _ _ d l r o w _ _ _ _
_ _ _ _ _ _ l _ _ _ _ _
_ _ _ _ _ _ _ o _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _

请通过为函数设置一些示例输入,将您的代码转换为正确的代码,这样我们就可以在无需额外工作的情况下重现您的问题。我无法判断您的问题是源于您显示的代码,还是您使用输出网格执行的缺少打印。另外,下次请用泛型[python]标记和特定于版本的标记来标记您的问题。@AndrasDeak我已经把图像的链接放在这里了。很抱歉给您带来不便这没关系,但您完全错过了我之前评论中的要点。请通过为您的函数设置一些示例输入,使您的代码成为正确的,这样我们就可以在无需额外工作的情况下重现您的问题。我无法判断您的问题是源于您显示的代码,还是您使用输出网格执行的缺少打印。另外,下次请用泛型[python]标记和特定于版本的标记来标记您的问题。@AndrasDeak我已经把图像的链接放在这里了。很抱歉给您带来不便没关系,但您完全没有领会我之前评论的要点。哦,非常感谢。我不知道单间距字体。哦,非常感谢。我不知道单间距字体。