Python if-elif语句命令 [英] Python if-elif statements order

查看:134
本文介绍了Python if-elif语句命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个看似非常基本和简单的问题,但我无法找到一个合适而优雅的方法来解决它。

I've encountered a problem that seems pretty much basic and simple and however I can't figure out a proper and elegant way how to solve it.

情况是:有一个球员可以移动 - 让我们说向上。移动时他可能遇到一些障碍 - 让我们说树木。并且他可以使用一个非常简单的算法绕过它们:

The situation is: there's a player who can move - let's say upward. While moving he can encounter some obstacles - let's say trees. And he can bypass them using a pretty simple algorithm like this one:

if   <the obstacle has a free tile on its RIGHT>:
  move_right() 
elif <the obstacle has a free tile on its LEFT>:
  move_left()
else:
  stop()

嗯,它运作完美,但有一个缺点:如果障碍物的右侧和左侧都有免费的瓷砖,那么它可以从双方绕过,玩家总是从右边绕过它。它几乎可以解释,但仍然不那么酷。

Well, it works perfectly, but there's a drawback: if the obstacle has free tiles both from its right and left so it can be bypassed from the both sides, the player always bypasses it from the right. It's pretty much explainable, but still not that cool.

这个想法是添加一些变化并以某种方式随机化玩家检查瓷砖可用性的顺序,如果两者都是是自由的,他可以不一定向右移动,而是随机移动。而且我必须承认我无法想出如何以简单而美观的方式做到这一点。

The idea is to add some variety and randomize somehow the order in which the player checks the availability of tiles so if both are free he could move not necessarily to the right, but in random direction. And I must admit I cannot come up with an idea how to do it in a simple and beautiful way.

基本上,解决方案应该是这样的......

Basically, the solution should probably be something like this...

if random(0, 1) == 0:
  if   <the obstacle has a free tile on its RIGHT>:
    move_right() 
  elif <the obstacle has a free tile on its LEFT>:
    move_left()
  else:
    stop()
else:
  if   <the obstacle has a free tile on its LEFT>:
    move_left()
  elif <the obstacle has a free tile on its RIGHT>:
    move_right() 
  else:
    stop()

但我猜我不需要解释为什么它看起来不是最好的。 = /

but I guess I don't need to explain why it doesn't seem the best one. =/

推荐答案

您可以将所有可用路线放在列表中,然后使用 random.choice()

You can put all available directions in a list, then use random.choice() on that:

directions = []
if <the obstacle has a free tile on its RIGHT>:
    directions.append(move_right)
if <the obstacle has a free tile on its LEFT>:
    directions.append(move_left)

if not directions:
    stop()
else:
    random.choice(directions)()  # pick an available direction at random

方向列表中将包含0,1或2个函数引用;如果它是空的,则没有选项,你调用 stop(),否则你从列表中随机选择并调用被挑选的函数。

The directions list will then have either 0, 1 or 2 function references in it; if it is empty, there were no options and you call stop(), otherwise you randomly pick from the list and call the picked function.

因为 random.choice()引发 IndexError 如果输入列表为空,你可以也可以使用:

Because random.choice() raises IndexError if the input list is empty, you could make use tof that too:

try:
    # pick an available direction at random
    random.choice(directions)()
except IndexError:
    # no directions available
    stop()

这篇关于Python if-elif语句命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆