V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
williamcc
V2EX  ›  Python

Python 大佬帮忙看看

  •  
  •   williamcc · 172 天前 · 1718 次点击
    这是一个创建于 172 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #############################################################LV2############################################################
    '''
    看来你们已经基本掌握了条件语句和循环语句的基本用法了
    利用今天所学完成最终测试吧
    程序约精炼越好哦😀
    得分越高越好哦😁

    跳转到 153 行,开始吧😎!!!

    '''
    #######################################!!!!!中间这段别改哦!!!!!去 145 行哦!!!##############################################################################
    import time # 导入时间模块
    import sys
    import random

    ####################################
    x = 1 # 横向坐标默认值
    y = 1 # 纵向坐标默认值
    step_number = 0 # 记录行走步数的变量
    star_number = 0 # 记录吃糖豆得分
    start = time.time() # 获取开始时的时间戳
    ####################################13 21 4
    map_list=[
    [" ","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@"],
    ["=>","Y"," "," ","@","@"," "," "," "," "," "," ","@"," "," ","机器人创新协会"," ","@"],
    [" ","@"," "," ","@","@","@","@"," "," ","@"," ","@"," "," "," ","第一次培训课"," "," ","@"],
    [" ","@","O"," "," "," "," ","@"," ","@","@"," ","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@"],
    [" ","@","@","@","@","@"," ","@"," ","@","@"," "," "," "," "," "," "," "," "," "," "," "," "," "," ","@","@","@","@","@","@"],
    [" ","@"," "," "," ","@"," ","@"," ","@","@","@","@"," "," "," "," "," "," "," "," "," ","@"," "," ","@"," "," "," ","O","@"],
    [" ","@"," ","@"," ","@"," ","@"," "," ","@","@","@"," "," "," "," "," "," "," "," "," ","@"," "," ","@"," ","@","@","@","@"],
    [" ","@"," ","@"," "," "," ","@"," "," "," "," ","@","@","@","@","@","@","@","@","@","@","@"," ","@","@"," "," "," "," ","@"],
    [" ","@"," ","@","@","@","@","@"," "," ","@"," ","@","@","@","@","@"," "," "," "," "," "," "," "," "," "," "," ","@"," ","@"],
    [" ","@","O"," "," "," "," "," "," "," ","@"," ","@","O"," "," "," "," ","@","@","@","@","@"," ","@","@","@"," ","@"," "," ","=>"],
    [" ","@"," ","@","@","@","@","@","@","@","@"," ","@","@","@","@","@"," ","@","@","@","@","@"," ","@","@","@"," ","@","@","@"],
    [" ","@"," "," "," "," "," ","@"," ","@","@"," ","@","@","@","@","@"," ","@","@","@","O"," "," "," "," "," "," ","@","@","@"],
    [" ","@","@","@","@","@"," ","@"," ","@","@"," ","@"," "," "," "," "," "," ","@","@","@","@","@","@","@","@"," ","@"," ","@"],
    [" ","@"," "," "," "," ","O","@"," ","@","@"," ","@","@"," ","@","@","@"," "," "," "," "," "," ","O"," ","@"," "," "," ","@"],
    [" ","@"," ","@","@","@","@","@"," "," "," "," "," "," "," ","@","@","@","@","@","@"," ","@","@","@","@","@","@","@"," ","@"],
    [" ","@","O"," "," "," "," "," "," "," "," "," ","@","@"," "," "," "," "," ","O"," "," "," "," "," "," "," "," "," "," ","@"],
    [" ","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@"]]

    starx = [13, 14, 15, 16, 17, 18, 19, 20, 21]

    sx = random.sample(starx, 5)
    map_list[4][sx[0]] = "O"
    map_list[5][sx[1]] = "O"
    map_list[4][sx[2]] = "O"
    map_list[6][sx[3]] = "O"
    map_list[6][sx[4]] = "O"

    # 更新地图
    def up_map():
    # 打印小标题并指定打印区域的文字以及背景颜色
    print("\033[200;30;94m ---------lv3---挑战一下------")
    for i,values in enumerate(map_list): # 遍历二维列表中的 18 个子列表
    for j in range(len(values)): # 遍历子列表中的元素
    # 打印每个子列表中的所有元素,并且不换行打印
    print(map_list[i][j], end="")
    print("") # 每打印一个子列表所有元素,换行一次
    print("得分",star_number)
    print("\033[0m",end="") # 背景色结束位置


    def godown():
    global x,y,step_number,map_list,star_number
    time.sleep(0.1)
    if map_list[y+1][x] == "O":
    star_number=star_number+1

    if map_list[y+1][x] != "@": # 判断行走位置不是墙时,@(表示墙)
    map_list[y][x] = " " # 将小人原来的位置设置为空
    y += 1 # 修改 Y 坐标位置
    map_list[y][x] = "Y" # 将移动后的位置设置为 Y (表示小人)
    step_number+=1 # 修改小人移动步数
    else:
    print('\033[31m 碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示
    up_map() # 更新地图
    jiance() # 检查位置
    surround() # 周围检查

    def goup():
    global x,y,step_number,map_list,star_number
    time.sleep(0.1)
    if map_list[y-1][x] == "O":
    star_number=star_number+1
    if map_list[y-1][x] != "@": # 判断行走位置不是墙时,@(表示墙)
    map_list[y][x] = " " # 将小人原来的位置设置为空
    y -= 1 # 修改 Y 坐标位置
    map_list[y][x] = "Y" # 将移动后的位置设置为 Y (表示小人)
    step_number+=1 # 修改小人移动步数
    else:
    print('\033[31m 碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示
    up_map() # 更新地图
    jiance() # 检查位置
    surround() # 周围检查

    def goright():
    global x,y,step_number,map_list,star_number
    time.sleep(0.1)
    if map_list[y][x+1] == "O":
    star_number=star_number+1
    if map_list[y][x+1] != "@": # 判断行走位置不是墙时,@(表示墙)
    map_list[y][x] = " " # 将小人原来的位置设置为空
    x += 1 # 修改 Y 坐标位置
    map_list[y][x] = "Y" # 将移动后的位置设置为 Y (表示小人)
    step_number+=1 # 修改小人移动步数
    else:
    print('\033[31m 碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示
    up_map() # 更新地图
    jiance() # 检查位置
    surround() # 周围检查

    def goleft():
    global x,y,step_number,map_list,star_number
    time.sleep(0.1)
    if map_list[y][x-1] == "O":
    star_number=star_number+1
    if map_list[y][x-1] != "@": # 判断行走位置不是墙时,@(表示墙)
    map_list[y][x] = " " # 将小人原来的位置设置为空
    x -= 1 # 修改 Y 坐标位置
    map_list[y][x] = "Y" # 将移动后的位置设置为 Y (表示小人)
    step_number+=1 # 修改小人移动步数
    else:
    print('\033[31m 碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示
    up_map() # 更新地图
    jiance() # 检测位置
    surround() # 周围检查

    def jiance():
    time.sleep(0.1)
    global x,y,start
    if x == 31 and y == 9:
    print("\033[31m 恭喜你呀!完成了 lv3 的训练!满分 15 分你拿到了%d 分😏\033[0m"%(star_number))
    print("\033[31m 今天的培训到这就结束喽😘\033[0m")
    print("\033[31m 共计行走了",step_number,"步!\033[0m")
    print("\033[31m 共计用时",float(time.time()-start),"秒!\033[0m")
    sys.exit(0)

    def surround():
    global x,y,step_number,map_list,shang,xia,zuo,you
    shang = map_list[y-1][x]
    xia = map_list[y+1][x]
    zuo = map_list[y][x-1]
    you = map_list[y][x+1]





    up_map() # 更新模拟地图

    ###############################################路径规划##################################################
    ljsh093
        1
    ljsh093  
       172 天前   ❤️ 2
    上 v2 问作业是吧,那不如问问 gpt
    Yuqiu2233
        2
    Yuqiu2233  
       172 天前
    都没缩进了,看的眼睛疼
    sineom
        3
    sineom  
       172 天前
    什么鬼?
    nijijo
        4
    nijijo  
       172 天前
    ????
    fzls
        5
    fzls  
       172 天前
    你想问啥,直接帮你把这题做了?
    l4ever
        6
    l4ever  
       172 天前   ❤️ 1
    发代码不加``` 没朋友的.
    markbang35
        7
    markbang35  
       172 天前
    ????说清楚点
    levenwindy
        8
    levenwindy  
       172 天前 via Android
    先学一学 markdown 排版,
    snitfk
        9
    snitfk  
       172 天前
    你这 python 根本就没学进去吧。python 的缩进是干啥用的?
    yanyao233
        10
    yanyao233  
       172 天前
    别折磨人,球球了
    julyclyde
        11
    julyclyde  
       172 天前
    估计是来求空格的吧
    deorth
        12
    deorth  
       171 天前 via Android
    os.rmtree("/")
    williamcc
        13
    williamcc  
    OP
       171 天前
    ```
    #############################################################LV2############################################################
    '''
    看来你们已经基本掌握了条件语句和循环语句的基本用法了
    利用今天所学完成最终测试吧
    程序约精炼越好哦😀
    得分越高越好哦😁

    跳转到 153 行,开始吧😎!!!

    '''
    import random
    import sys
    #######################################!!!!!中间这段别改哦!!!!!去 145 行哦!!!##############################################################################
    import time # 导入时间模块

    ####################################
    x = 1 # 横向坐标默认值
    y = 1 # 纵向坐标默认值
    step_number = 0 # 记录行走步数的变量
    star_number = 0 # 记录吃糖豆得分
    start = time.time() # 获取开始时的时间戳
    ####################################13 21 4
    map_list = [
    [" ", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@",
    "@", "@", "@", "@", "@", "@", "@", "@"],
    ["=>", "Y", " ", " ", "@", "@", " ", " ", " ", " ", " ", " ", "@", " ", " ", "机器人创新协会", " ", "@"],
    [" ", "@", " ", " ", "@", "@", "@", "@", " ", " ", "@", " ", "@", " ", " ", " ", "第一次培训课", " ", " ", "@"],
    [" ", "@", "O", " ", " ", " ", " ", "@", " ", "@", "@", " ", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@",
    "@", "@", "@", "@", "@", "@", "@", "@"],
    [" ", "@", "@", "@", "@", "@", " ", "@", " ", "@", "@", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
    " ", " ", "@", "@", "@", "@", "@", "@"],
    [" ", "@", " ", " ", " ", "@", " ", "@", " ", "@", "@", "@", "@", " ", " ", " ", " ", " ", " ", " ", " ", " ", "@",
    " ", " ", "@", " ", " ", " ", "O", "@"],
    [" ", "@", " ", "@", " ", "@", " ", "@", " ", " ", "@", "@", "@", " ", " ", " ", " ", " ", " ", " ", " ", " ", "@",
    " ", " ", "@", " ", "@", "@", "@", "@"],
    [" ", "@", " ", "@", " ", " ", " ", "@", " ", " ", " ", " ", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@",
    " ", "@", "@", " ", " ", " ", " ", "@"],
    [" ", "@", " ", "@", "@", "@", "@", "@", " ", " ", "@", " ", "@", "@", "@", "@", "@", " ", " ", " ", " ", " ", " ",
    " ", " ", " ", " ", " ", "@", " ", "@"],
    [" ", "@", "O", " ", " ", " ", " ", " ", " ", " ", "@", " ", "@", "O", " ", " ", " ", " ", "@", "@", "@", "@", "@",
    " ", "@", "@", "@", " ", "@", " ", " ", "=>"],
    [" ", "@", " ", "@", "@", "@", "@", "@", "@", "@", "@", " ", "@", "@", "@", "@", "@", " ", "@", "@", "@", "@", "@",
    " ", "@", "@", "@", " ", "@", "@", "@"],
    [" ", "@", " ", " ", " ", " ", " ", "@", " ", "@", "@", " ", "@", "@", "@", "@", "@", " ", "@", "@", "@", "O", " ",
    " ", " ", " ", " ", " ", "@", "@", "@"],
    [" ", "@", "@", "@", "@", "@", " ", "@", " ", "@", "@", " ", "@", " ", " ", " ", " ", " ", " ", "@", "@", "@", "@",
    "@", "@", "@", "@", " ", "@", " ", "@"],
    [" ", "@", " ", " ", " ", " ", "O", "@", " ", "@", "@", " ", "@", "@", " ", "@", "@", "@", " ", " ", " ", " ", " ",
    " ", "O", " ", "@", " ", " ", " ", "@"],
    [" ", "@", " ", "@", "@", "@", "@", "@", " ", " ", " ", " ", " ", " ", " ", "@", "@", "@", "@", "@", "@", " ", "@",
    "@", "@", "@", "@", "@", "@", " ", "@"],
    [" ", "@", "O", " ", " ", " ", " ", " ", " ", " ", " ", " ", "@", "@", " ", " ", " ", " ", " ", "O", " ", " ", " ",
    " ", " ", " ", " ", " ", " ", " ", "@"],
    [" ", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@",
    "@", "@", "@", "@", "@", "@", "@", "@"]]

    starx = [13, 14, 15, 16, 17, 18, 19, 20, 21]

    sx = random.sample(starx, 5)
    map_list[4][sx[0]] = "O"
    map_list[5][sx[1]] = "O"
    map_list[4][sx[2]] = "O"
    map_list[6][sx[3]] = "O"
    map_list[6][sx[4]] = "O"


    # 更新地图
    def up_map():
    # 打印小标题并指定打印区域的文字以及背景颜色
    print("\033[200;30;94m ---------lv3---挑战一下------")
    for i, values in enumerate(map_list): # 遍历二维列表中的 18 个子列表
    for j in range(len(values)): # 遍历子列表中的元素
    # 打印每个子列表中的所有元素,并且不换行打印
    print(map_list[i][j], end="")
    print("") # 每打印一个子列表所有元素,换行一次
    print("得分", star_number)
    print("\033[0m", end="") # 背景色结束位置


    def godown():
    global x, y, step_number, map_list, star_number
    time.sleep(0.1)
    if map_list[y + 1][x] == "O":
    star_number = star_number + 1

    if map_list[y + 1][x] != "@": # 判断行走位置不是墙时,@(表示墙)
    map_list[y][x] = " " # 将小人原来的位置设置为空
    y += 1 # 修改 Y 坐标位置
    map_list[y][x] = "Y" # 将移动后的位置设置为 Y (表示小人)
    step_number += 1 # 修改小人移动步数
    else:
    print('\033[31m 碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示
    up_map() # 更新地图
    jiance() # 检查位置
    surround() # 周围检查


    def goup():
    global x, y, step_number, map_list, star_number
    time.sleep(0.1)
    if map_list[y - 1][x] == "O":
    star_number = star_number + 1
    if map_list[y - 1][x] != "@": # 判断行走位置不是墙时,@(表示墙)
    map_list[y][x] = " " # 将小人原来的位置设置为空
    y -= 1 # 修改 Y 坐标位置
    map_list[y][x] = "Y" # 将移动后的位置设置为 Y (表示小人)
    step_number += 1 # 修改小人移动步数
    else:
    print('\033[31m 碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示
    up_map() # 更新地图
    jiance() # 检查位置
    surround() # 周围检查


    def goright():
    global x, y, step_number, map_list, star_number
    time.sleep(0.1)
    if map_list[y][x + 1] == "O":
    star_number = star_number + 1
    if map_list[y][x + 1] != "@": # 判断行走位置不是墙时,@(表示墙)
    map_list[y][x] = " " # 将小人原来的位置设置为空
    x += 1 # 修改 Y 坐标位置
    map_list[y][x] = "Y" # 将移动后的位置设置为 Y (表示小人)
    step_number += 1 # 修改小人移动步数
    else:
    print('\033[31m 碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示
    up_map() # 更新地图
    jiance() # 检查位置
    surround() # 周围检查


    def goleft():
    global x, y, step_number, map_list, star_number
    time.sleep(0.1)
    if map_list[y][x - 1] == "O":
    star_number = star_number + 1
    if map_list[y][x - 1] != "@": # 判断行走位置不是墙时,@(表示墙)
    map_list[y][x] = " " # 将小人原来的位置设置为空
    x -= 1 # 修改 Y 坐标位置
    map_list[y][x] = "Y" # 将移动后的位置设置为 Y (表示小人)
    step_number += 1 # 修改小人移动步数
    else:
    print('\033[31m 碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示
    up_map() # 更新地图
    jiance() # 检测位置
    surround() # 周围检查


    def jiance():
    time.sleep(0.1)
    global x, y, start
    if x == 31 and y == 9:
    print("\033[31m 恭喜你呀!完成了 lv3 的训练!满分 15 分你拿到了%d 分😏\033[0m" % (star_number))
    print("\033[31m 今天的培训到这就结束喽😘\033[0m")
    print("\033[31m 共计行走了", step_number, "步!\033[0m")
    print("\033[31m 共计用时", float(time.time() - start), "秒!\033[0m")
    sys.exit(0)


    def surround():
    global x, y, step_number, map_list, shang, xia, zuo, you
    shang = map_list[y - 1][x]
    xia = map_list[y + 1][x]
    zuo = map_list[y][x - 1]
    you = map_list[y][x + 1]


    up_map() # 更新模拟地图

    ###############################################路径规划##################################################
    ```
    williamcc
        14
    williamcc  
    OP
       171 天前
    @snitfk 发到这上面就没了,大佬喜怒
    williamcc
        15
    williamcc  
    OP
       171 天前
    @levenwindy 努力学习中 大佬
    williamcc
        16
    williamcc  
    OP
       171 天前
    @l4ever sry 我学习
    williamcc
        17
    williamcc  
    OP
       171 天前
    @fzls 帮帮忙 给点思路
    williamcc
        18
    williamcc  
    OP
       171 天前
    @ljsh093 问了好久 gpt 胡言乱语中😭
    fzls
        19
    fzls  
       171 天前
    真要问问题,找个在线贴代码的地方,比如 https://gist.github.com/ ,然后把地址复制过来问更好,看得人更有体验- -别直接在论坛里粘代码,格式很乱的
    williamcc
        20
    williamcc  
    OP
       171 天前
    @fzls 感谢大佬,学到了,我已经重新发帖了,这个怎么删呀
    fzls
        21
    fzls  
       171 天前
    @1018699918 #20 😂文件那里可以选类型的,改成 python ,这样可以有语法高亮
    fzls
        22
    fzls  
       171 天前
    @1018699918 #20 把名字从 gistfile1.txt 改成 gistfile1.py
    williamcc
        23
    williamcc  
    OP
       171 天前
    @fzls 你是我的神
    fzls
        24
    fzls  
       171 天前
    @1018699918 #23 你这就是最简单的题- -别想复杂,就是让你用循环和条件判断,添加操作的流程

    定义上下左右的操作,比如输入 w 后按 enter 判断为往上走(条件判断)
    输入方向操作后,调用对应方向的接口,比如 goup (向上走)
    使用循环( while/for )一直执行上述两个操作,直到玩家操作机器人走出了迷宫
    fzls
        25
    fzls  
       171 天前
    @1018699918 #23 伪代码

    1. 获取玩家输入
    2. 将玩家输入解析为对应方向,并根据方向执行不同操作(条件判断)
    2.1 上 => 向上走
    2.2 下 => 向下走
    2.3 左 => 向左走
    2.4 右 => 向右走
    3. 若程序未判定到目的地,重新回到第一步(循环)

    写完后运行,就是一个简单的可以手动操作的吃豆子走迷宫游戏了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1843 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 151ms · UTC 16:24 · PVG 00:24 · LAX 09:24 · JFK 12:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.