简体   繁体   English

是否有与bash脚本中的重音符号相当的Python?

[英]Is there a Python equivalent to the grave accent in bash scripting?

I don't like using a lot of IF statements. 我不喜欢使用很多IF语句。 Here is what I am playing around with right now, in the world of having to use a lot of IF statements to get it done: 在必须使用大量IF语句来完成任务的世界中,这就是我现在正在处理的事情:

First, some variables... 首先,一些变量...

pick_level = 1
lockpicking_level1_maxxp = 40
lockpicking_level2_maxxp = 70
lockpicking_level3_maxxp = 100

Now we get to picking a lock. 现在我们来挑选一把锁。 A successful pick happens when a random number between 1 and anything up to the current lockpicking skill level's max xp divided by 2 is chosen. 如果选择了介于1到当前锁选技巧级别的最大xp除以2之间的任意数字之间的随机数,则表示成功选择。 So for level 1 it's anything between 1 and 20 - level 2, anything between 1 and 35, and level 3 anything between 1 and 50. Here's how it would look with IF statements: 因此,对于1级,它介于1到20之间-2级,介于1和35之间,而3级则介于1和50之间。这是IF语句的外观:

x = random.randint(1, 100)
if pick_level == 1:
  if x not in range(1, (lockpicking_level1_maxxp / 2)):
    print 'You failed to pick the lock'
    ...
elif pick_level == 2:
  if x not in range(1, (lockpicking_level2_maxxp / 2)):
    etcetc

What I'd like to do is determine the xp based on the value of pick_level and not have to do a bunch of IF statements asking the same question over and over. 我想做的是基于pick_level的值确定xp,而不必一堆又一遍地询问相同问题的IF语句。 It would go down as something like this (using grave accents like in bash): 它会像这样(使用bash中的重音)下降:

if x not in range(1, (lockpicking_level`pick_level`_maxxp / 2)):

This way I can avoid all the IF statements by having the code automatically become 'lockpicking_level1_maxxp', 'lockpicking_level2_maxxp' or 'lockpicking_level3_maxxp' based on whatever pick_level's value is. 通过这种方式,我可以根据任何pick_level的值将代码自动变为“ lockpicking_level1_maxxp”,“ lockpicking_level2_maxxp”或“ lockpicking_level3_maxxp”来避免所有IF语句。

It has been a while since I've done bash scripts though, so my memory of how it works may be off a bit. 自从我完成了bash脚本以来已经有一段时间了,所以我对它的工作方式的记忆可能有些不足。 Also I know `` in Python up to version 3 is basically the same as using repr() so that wouldn't be how to do it. 我也知道``在Python 3之前的版本中,基本上与使用repr()相同,所以那不是怎么做的。 Is there any way I can do this or am I stuck writing ugly IF statements all over the place? 有什么办法可以做到?还是我到处都写丑陋的IF语句?

I suppose I could use a dict, as such: 我想我可以这样使用dict:

lockpicking_maxxp = {1:20, 2:35, 3:50}
...
if x in range(1, lockpicking_maxxp[pick_level]):

...but I don't know if that is exactly how I will be setting up my skill database (although it will probably end up being a JSON file. I love JSON.) ...但是我不知道这是否就是我要建立技能数据库的方式(尽管它最终可能会变成JSON文件。我喜欢JSON。)

Thank you very much for your time. 非常感谢您的宝贵时间。 I look forward to learning more about this idea. 我期待更多地了解这个想法。

Don't repeat yourself . 不要重复自己 Use data structures instead of individual objects as much as possible: 尽可能使用数据结构代替单个对象:

lockpicking_maxxp = {
    1: 40,
    2: 70,
    3: 100
}

x = random.randint(1, 100)
if x > lockpicking_maxxp[pick_level] / 2:
    print 'You failed to pick the lock'
    ...

Of course, you probably have other skills in this game, so you might want something more like: 当然,您可能在此游戏中还有其他技能,因此您可能想要更多类似的东西:

maxxp = {
    'lockpicking': {
        1: 40,
        2: 70,
        3: 100
    }, 'trap_searching': {
        1: 50,
        ...
    }, ...
}

skill = 'lockpicking'
x = random.randint(1, 100)
if x > maxxp[skill][level[skill]] / 2:
    print 'You failed to pick the lock'
    ...

You can use locals() to get the local symbol table as a dict and use it to get the variable value. 您可以使用locals()获取本地符号表作为字典,并使用它来获取变量值。 ie: 即:

>>> pick_level = 1
>>> lockpicking_level1_maxxp = 40
>>> lockpicking_level2_maxxp = 70
>>> lockpicking_level3_maxxp = 100
>>> locals()["lockpicking_level%d_maxxp" % pick_level]
40

Whenever you feel the need to add numbers to your variables you should probably just use a list: 每当您需要在变量中添加数字时,就应该只使用一个列表:

pick_level = 1
lockpicking_levels = [0,40, 70, 100]

test = random.randint(1, 100)

if test < lockpicking_levels[pick_level] / 2:
    print "you failed"
else:
    ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM