Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 如何使我的脚本停止,如果;“人”;到达边界?_Python_Python 3.x_Terminal - Fatal编程技术网

Python 如何使我的脚本停止,如果;“人”;到达边界?

Python 如何使我的脚本停止,如果;“人”;到达边界?,python,python-3.x,terminal,Python,Python 3.x,Terminal,我正在尝试制作一个网格,在这个网格中一个人可以移动,但只有当他到达边界时才会停止 他可以到达边界并停下来,或者继续行走,直到文件中没有更多字符。我不知道如何接近停车的人;我是一个初学者,不知道是否应该使用数组,因为每次运行脚本时边界(长度和宽度)都应该是随机的(除了东墙每次有一个5瓦的门) try:#文件参数 text=打开(文件名) lines=text.readlines() text.close() 除: 打印(“错误缺少读取权限!”,文件=sys.stderr) 退出() 匹配=重新搜

我正在尝试制作一个网格,在这个网格中一个人可以移动,但只有当他到达边界时才会停止

他可以到达边界并停下来,或者继续行走,直到文件中没有更多字符。我不知道如何接近停车的人;我是一个初学者,不知道是否应该使用数组,因为每次运行脚本时边界(长度和宽度)都应该是随机的(除了东墙每次有一个5瓦的门)

try:#文件参数
text=打开(文件名)
lines=text.readlines()
text.close()
除:
打印(“错误缺少读取权限!”,文件=sys.stderr)
退出()
匹配=重新搜索(“[nsew]”,文件名)
如果匹配:
如果文件名中有“n”:
y+=1
文件名中的elif's':
y-=1
文件名中的elif“e”:
x+=1
其他:
x-=1
其他:
打印(“文件名包含n、s、e和w以外的字符。请使用仅包含这些字符的文件。”)
退出()
如果x==0且y==0:
计数器+=1
其他:
大象=1#占位符
如果x==((长度-1)/2)和-22和y<-2:
打印(“步行“步”长,返回原点“柜台”次。结束是因为醉汉撞到了墙。”)
其他:
打印(“步行“步”长,返回原点“柜台”次。结束是因为醉汉没有步了。”)

如何准确地表示“person”呢?它只是一个根据文件中的顺序四处走动的人。首先,收缩
try/except
子句,只包含
open
语句。它屏蔽了所有潜在的错误。谢谢,找到了一个被try/except子句屏蔽的语法错误
    try: #file argument
        text = open(filename)
        lines = text.readlines()
        text.close()
    except:
        print("Error lacks reading permissions!", file=sys.stderr)
        exit()
    match=re.search("[nsew]", filename)
    if match:
        if 'n' in filename:
            y+=1
        elif 's' in filename:
            y-=1
        elif 'e' in filename:
            x+=1
        else:
            x-=1
    else:
        print("filename contains characters other than n, s, e and w. Please use a file that only contains these characters.")
        exit()
    if x == 0 and y == 0:
        counter+=1
    else:
        elephant=1 #placeholder
    if x == ((length-1)/2) and -2 < y < 2:
        print ("The walk was" steps "long, returned to the origin" counter "times. It ended because the drunk man reached the gate.")
    elif x == ((length-1)/2) and y > 2 and y < -2:
        print ("The walk was" steps "long, returned to the origin" counter "times. It ended because the drunk man reached the wall.")
    else:
        print ("The walk was" steps "long, returned to the origin" counter "times. It ended because the drunk man ran out of steps.")