小编Fre*_*e R的帖子

如何在Python中舍入到特定值

我正在研究一种自动为角色扮演游戏创建角色表的算法.在游戏中,你有一些属性,你可以用它来增加它们.但是,在某个值处,需要2个点才能将实际属性的值增加1.您可以使用一定数量的点开始,默认情况下每个属性的值为1

我有一个随机分配点的程序,但是我不知道如何在必要时将这些值(在字典中)更改为舍入.

例如,如果我在"强度"中加入3分,那很好,我的"强度"值为3(包括基数1).但是,如果我输入4个点,我仍然应该只有4个值.它应该取5个点(加上基数1)以获得值5.然后需要另外2个点来获得值6,3点得到7值和3点得到值8.

我目前用来分配attibutes的代码如下所示:

attributes = {}
row1 = ['strength', 'intelligence', 'charisma']
row2 = ['stamina', 'willpower']
row3 = ['dexterity', 'wits', 'luck']

def assignRow(row, p): # p is the number of points you have to assign to each row
    rowValues = {}
    for i in range(0, len(row)-1):
        val = randint(0, p)
        rowValues[row[i]] = val + 1
        p -= val
    rowValues[row[-1]] = p + 1
    return attributes.update(rowValues)

assignRow(row1, 7)
assignRow(row2, 5)
assignRow(row3, 3)
Run Code Online (Sandbox Code Playgroud)

我想要的只是一个简单的函数,它将字典"attributes"作为参数,并将每个属性所具有的点数转换为它应该具有的正确值.

"strength": 4保持"strength": 4,但"wits": …

python function rounding

10
推荐指数
1
解决办法
691
查看次数

如何在 Python 中构建 Brainfuck 解释器?

我一直在研究 BF 解释器,试图确保它不使用外部库,并且在单个函数中工作。

我遇到的问题是有些程序运行得很好,而有些则不然。这使得调试和弄清楚出了什么问题变得很困难。

共同的因素似乎是它无法处理带有多个括号的 BF 程序(尽管有一些例外,但是程序可以工作,只是不完全)。

编码:

def interpret(code):
    array = [0]
    pointerLocation = 0
    i = 0
    c = 0
    print(code)
    while i < len(code):
        if code[i] == '<':
            if pointerLocation > 0:
                pointerLocation -= 1
        elif code[i] == '>':
            pointerLocation += 1
            if len(array) <= pointerLocation:
                array.append(0)
        elif code[i] == '+':
            array[pointerLocation] += 1
        elif code[i] == '-':
            if array[pointerLocation] > 0:
                array[pointerLocation] -= 1
        elif code[i] == '.':
            print(array[pointerLocation], chr(array[pointerLocation]))
        elif code[i] == ',':
            x = …
Run Code Online (Sandbox Code Playgroud)

python interpreter brainfuck

4
推荐指数
1
解决办法
3084
查看次数

Powershell中的'For'循环时间条件

我正在寻找的是Powershell版本:shell中的时间条件循环

所以我可以这样:

if(Condition -eq True){
    for(3000){         #I assume it will use milliseconds
        COMMAND
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:这不会连续运行,不像示例,它可以随时触发,当时间结束时,它应该退出循环并恢复正常的程序.

powershell

2
推荐指数
1
解决办法
139
查看次数

用Infuenced字符串替换Javascript中的所有子字符串

我期待输入看起来像:"x ^ 3"并且想用一串"pow(x,3)"替换它.然而,整个输入可能看起来像"x ^ 3 + x ^ 2 + 4".在这里,我希望"^"运算符的每个实例代表转换为"pow()"子字符串.

这意味着我替换它的子字符串会受到它所发现的内容的影响,但如果我使用正则表达式,我需要"^"的任意一侧的通配符操作符.

有没有办法"存储"通配符在给定的实例中重新插入替换字符串中的内容?

即在x ^ 3中,3被存储并放回到pow(x,3)中

案例:

"x ^ 2" - >"pow(x,2)"

"x ^ 3 + x ^ 2" - >"pow(x,3)+ pow(x,2)"

"x ^ 4 + y ^ 19" - >"pow(x,4)+ pow(y,19)"

javascript regex

0
推荐指数
1
解决办法
78
查看次数