[英]While Not Loop running more times than intended
I have a problem where I am trying to make a word search puzzle generator and I have run into multiple problems throughout the day.我在尝试制作单词搜索拼图生成器时遇到了问题,并且一整天都遇到了多个问题。 (Thanks to this community I've solved most of them!) (感谢这个社区,我已经解决了大部分问题!)
I have another problem now where I have a loop that should find a spot to place a word, place it, and move on to the next word.我现在有另一个问题,我有一个循环,应该找到一个位置来放置一个单词,放置它,然后移动到下一个单词。 Instead it is finding all possible spots the word can be and placing the word in all of them.相反,它是找到单词可能存在的所有可能位置,并将该单词放入所有位置。 I only want each word to appear once.我只希望每个单词出现一次。 I thought that the lines while not placed
and placed = true
would handle this but it isn't working.我认为while not placed
和placed = true
的线条placed = true
会处理这个问题,但它不起作用。
Thanks in advance for any help, and here is my code:在此先感谢您的帮助,这是我的代码:
import tkinter as tk
import random
import string
handle = open('dictionary.txt')
words = handle.readlines()
handle.close()
grid_size = 10
words = [ random.choice(words).upper().strip() \
for _ in range(5) ]
print ("The words are:")
print(words)
grid = [ [ '_' for _ in range(grid_size) ] for _ in range(grid_size) ]
orientations = [ 'leftright', 'updown', 'diagonalup', 'diagonaldown' ]
class Label(tk.Label):
def __init__(self, parent, **kwargs):
super().__init__(parent, **kwargs, font=("Courier", 44))
#self.bind('<Button-1>', self.on_click)
#def on_click(self, event):
#w = event.widget
#row, column = w.grid_info().get('row'), w.grid_info().get('column')
#print('coord:{}'.format((row, column)))
#w.destroy()
class App(tk.Tk):
def __init__(self):
super().__init__()
for row in range(grid_size):
for column in range(grid_size):
for word in words:
word_length = len(word)
placed = False
while not placed:
orientation = random.choice(orientations)
if orientation == 'leftright':
step_x = 1
step_y = 0
if orientation == 'updown':
step_x = 0
step_y = 1
if orientation == 'diagonalup':
step_x = 1
step_y = -1
if orientation == 'diagonaldown':
step_x = 1
step_y = 1
x_position = random.randrange(grid_size)
y_position = random.randrange(grid_size)
ending_x = x_position + word_length*step_x
ending_y = y_position + word_length*step_y
if ending_x < 0 or ending_x >= grid_size: continue
if ending_y < 0 or ending_y >= grid_size: continue
failed = False
for i in range(word_length):
character = word[i]
new_position_x = x_position + i*step_x
new_position_y = y_position + i*step_y
character_at_new_position = grid[new_position_x][new_position_y]
if character_at_new_position != '_':
if character_at_new_position == character:
continue
else:
failed = True
break
if failed:
continue
else:
for i in range(word_length):
character = word[i]
new_position_x = x_position + i*step_x
new_position_y = y_position + i*step_y
grid[new_position_x][new_position_y] = character
if ( grid[row][column] == grid[new_position_x][new_position_y] ):
grid[row][column] = grid[new_position_x][new_position_y]
Label(self, text=character).grid(row=row, column=column)
placed = True
#if ( grid[row][column] == '_' ):
#txt = random.SystemRandom().choice(string.ascii_uppercase)
#Label(self, text=txt).grid(row=row, column=column)
if __name__ == '__main__':
App().mainloop()
I can assure you that your expectation of while
loop is correct.我可以向您保证,您对while
循环的期望是正确的。
In [1]: placed = False
In [2]: i = 0
In [3]: while not placed:
...: i += 1
...: print(i)
...: if i == 5:
...: placed = True
...:
1
2
3
4
5
Given that, my suspicion is that your code always hit continue
, which means it never hits the statement placed = True
, hence infinite loop.鉴于此,我怀疑您的代码总是命中continue
,这意味着它永远不会命中语句placed = True
,因此是无限循环。 So I suggest you check if your condition to continue
is as expected.所以,我建议你检查你的病情continue
如预期。
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.