首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在if条件下,有些语句没有被执行,而有些语句正在执行。

在if条件下,有些语句没有被执行,而有些语句正在执行。
EN

Stack Overflow用户
提问于 2017-11-05 21:35:07
回答 2查看 94关注 0票数 1

我为在openai健身房随机扮演pacman写了一个小程序。但是,它的作用非常微弱,在if条件下只执行一条语句。游戏没有呈现(可能是因为env.reset())没有执行。

代码语言:javascript
复制
import gym

episode = 0
#episode_reward = 0
#running_reward = None
env = gym.make("MsPacman-v0")
env.reset()


while True:
    env.render()
    action = env.action_space.sample()
    #print (action)
    _, __, done, ___ = env.step(action)
    #print(reward)
    #episode_reward += reward

    if done:
        print('Game over')              #Why is this line not printed
        episode = episode + 1           #Why is episode not getting updated
        #running_reward = episode_reward if running_reward is None else  running_reward * 0.99 + episode_reward * 0.01
        #print('Episode %d, episode reward total was %f. running mean: %f' % (episode, episode_reward, running_reward))
        print('Episode, ', episode)      #Only this line is printed on screen
        #episode_reward = 0
        env.reset()

只是希望我没有犯什么愚蠢的错误!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-05 22:34:36

根据我们的讨论在聊天,您的文件中似乎混合了制表符和空格,这使得块在视觉上看起来是连续的,但是将其解释为两个单独的块(一个在if块中,另一个在其中)。

显然,这是因为while循环缩进了制表符,但是不包括print语句的if块的最后一部分只使用空格。它是这样的:

代码语言:javascript
复制
\t___if done:
\t___....print('Game over')
\t___....episode = episode + 1
 ........print('Episode ', episode)
 ........env.reset()       

因此,这被解释为相当于:

代码语言:javascript
复制
if done:
    # Start of block
    print('Game over')              #Why is this line not printed
    episode = episode + 1           #Why is episode not getting updated
    # End of block

print('Episode, ', episode)      #Only this line is printed on screen
#episode_reward = 0
env.reset()

如果黑手C竞赛有类似于python的东西,可能需要记住一些东西。

票数 1
EN

Stack Overflow用户

发布于 2017-11-05 22:30:57

您的代码混合了制表符和空格。(我通过单击帖子下面的“编辑”并将文本复制到编辑器进行检查。)

用空格替换所有的制表符,它会解决这个问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47126885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档