gpt4 book ai didi

python - 小改动使脚本挂起 - 不明白为什么

转载 作者:行者123 更新时间:2023-12-01 08:50:58 24 4
gpt4 key购买 nike

我做了一个单词搜索游戏。它会打印出一个包含一些单词的网格。代码如下:

import random
DirectionList = []

# Create a grid filled with "." representing a blank
def createGrid():
grid=[]
for row in range(15):
grid.append([])
for column in range(50):
grid[row].append(".")
return grid

# Print the grid to the screen
def printGrid(grid):
for row in range(len(grid)):
for column in range(len(grid[row])):
print(grid[row][column],end="")
print()

# Try to place the word. Return True if successful
# False if it failed and we need to try again.
def tryToPlaceWord(grid,word):
# Figure out the direction of the work.
# Change the 8 to a 7 if you don't want backwards
# words.
status_check = []
direction=random.randrange(0,8)
DirectionList.append(direction)
if( direction == 0 ):
x_change=-1
y_change=-1
if( direction == 1 ):
x_change=0
y_change=1
if( direction == 2 ):
x_change=1
y_change=-1
if( direction == 3 ):
x_change=1
y_change=0
if( direction == 4 ):
x_change=1
y_change=1
if( direction == 5 ):
x_change=0
y_change=1
if( direction == 6 ):
x_change=-1
y_change=1
if( direction == 7 ):
x_change=-1
y_change=0

# Find the length and height of the grid
height=len(grid)
width=len(grid[0])

# Create a random start point
column=random.randrange(width)
row=random.randrange(height)

# Check to make sure the word won't run off the edge of the grid.
# If it does, return False. We failed.
if( x_change < 0 and column < len(word) ):
status_check.append(False)
status_check.append("None")
return status_check
if( x_change > 0 and column > width-len(word) ):
status_check.append(False)
status_check.append("None")
return status_check
if( y_change < 0 and row < len(word) ):
status_check.append(False)
status_check.append("None")
return status_check
if( y_change > 0 and row > height-len(word) ):
status_check.append(False)
status_check.append("None")
return status_check

# Now check to make sure there isn't another letter in our way
current_column=column
current_row=row
for letter in word:
# Make sure it is blank, or already the correct letter.
if grid[current_row][current_column]==letter or grid[current_row][current_column]=='.':
current_row += y_change
current_column += x_change
else:
# Oh! A different letter is already here. Fail.
status_check.append(False)
status_check.append("None")
return status_check

# Everything is good so far, actually place the letters.
current_column=column
current_row=row
for letter in word:
grid[current_row][current_column]=letter
current_row += y_change
current_column += x_change
if 7 in DirectionList:
status_check.append(True)
status_check.append("True")
return status_check
else:
status_check.append(True)
status_check.append("None")
return status_check

# This just calls tryToPlaceWord until we succeed. It could
# repeat forever if there is no possible place to put the word.
def placeWord(grid,words):
for word in words:
succeed=False

while not(succeed):
status_check=tryToPlaceWord(grid,word)
succeed=status_check[0]
backward = status_check[1]
return backward

# Create an empty grid
grid = createGrid()

# A list of words
words = ["pandabear","fish","snake","porcupine","dog","cat","tiger","bird","alligator","ant","camel","dolphin"]

# Place some words
placeWord(grid,words)

backward = placeWord(grid,words)
print("Current status:\n"
"\tGenerating a word searching diagram...\n"
"\tWords :",len(words),"\n"
"\tBackword :",backward)

# Print it out
printGrid(grid)

然后,我想让代码打印出随机字母而不是点(.),所以我做了一些小改动。

import random
import string
DirectionList = []

# Create a grid filled random letters representing a blank
def createGrid():
grid=[]
for row in range(15):
grid.append([])
for column in range(50):
grid[row].append(random.choice(string.ascii_uppercase))
return grid

但是 python 停止运行,很长时间没有打印任何内容。我想也许python需要很多时间来生成15 x 50个随机字母,所以我将代码更改为grid[row].append("a"),但是python仍然无法运行。

出了什么问题?

编辑:

将代码更改为

for letter in word:
# Make sure it is blank, or already the correct letter.
if grid[current_row][current_column]==letter or grid[current_row][current_column]==' ':
current_row += y_change
current_column += x_change

但是代码仍然挂起......

最佳答案

您使用“.”还要检查网格中的单元格是否空闲(第 87 行),因此如果您用随机字母填充单元格,它们将不再空闲。您可以在输入单词后用随机字母填充空格...

关于python - 小改动使脚本挂起 - 不明白为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53131944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com