简体   繁体   English

为什么我的自动生成代码有时会卡在中间?

[英]Why is my auto generation code getting stuck mid way sometimes?

The code is as follows... 代码如下...

import random
file_name = 'room_gen'
room_no = 0
direction = 0
cont = True
method = "a"

while room_no != 6:
    last_dir = direction
    direction = random.randrange(1,5)
    print(direction)
    layout = random.randrange(2,6)
    if room_no == 0:
        direction = 'b'
        layout = 0
        method = "w"
    elif room_no == 5:
        layout = 1
    else:
        if direction == 1:
            direction = 'n'
        elif direction == 2:
            direction = 'e'
        elif direction == 3:
            direction = 's'
        elif direction == 4:
            direction = 'w'
    if direction == 'n' and last_dir == 's':
        cont = False
    elif direction == 's' and last_dir == 'n':
        cont = False
    elif direction == 'e' and last_dir == 'w':
        cont = False
    elif direction == 'w' and last_dir == 'e':
        cont = False
    if cont == True:
        direction = str(direction)
        layout = str(layout)
        room_no = str(room_no)
        file = open((file_name),(method))
        write = (direction + layout + room_no + "\n")
        file.write(write)
        room_no = int(room_no)
        file.close()
        room_no = room_no + 1
        method = "a"

file = open((file_name),"a")
write = ("x")
file.write(write)
file.close

It is meant to create a file like this and delete then replace the old text within the file when it is run again: 它的目的是创建一个这样的文件并删除,然后在再次运行时替换文件中的旧文本:

b00
n41
e22
s33
s44
w15
x

it works sometimes but sometimes its gets stuck and i have no idea why 它有时有效,但有时会卡住,我也不知道为什么

Chris_Rands,您使我意识到我要做的就是在while循环的开头添加cont = True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 while 循环有时会卡住,但有时不会在我的“秘密圣诞老人”程序中? - Why does my while loop gets stuck sometimes,but sometimes not in my "secret santa" program? 为什么我的代码有时会识别“?!” 作为符号,有时不是 - Why does my code sometimes identify "?!" as symbols and sometimes not 为什么这个正则表达式有时会卡住并冻结我的程序? 我可以使用什么替代方案? - Why does this regular expression sometimes get stuck and freeze my program? what alternative could i use? 有什么办法可以修复我的 python 代码被卡住的问题吗? - Is there any way to fix my python code being stuck? 为什么我的代码有时只注意到冲突? - Why does my code only sometimes notice collision? 为什么我的代码有时只能找到 html object? - Why does my code only sometimes find the html object? 为什么我的代码在抓取时陷入无限循环? - Why is my code stuck in an infinite loop when scraping? 为什么我的程序卡在线程上? - why is my program stuck on a thread? Python自动化抓取程序——有时会得到511错误代码有时不会 - Python Automation Scraping Program - Sometimes Getting 511 Error Code And Sometimes Not 为什么控制台卡在我的最后一个函数上来解决 python 中的 9X9 数独板? - Why is the console getting stuck on my last function to solve a 9X9 sudoku board in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM