返回原始值,python

时间:2014-12-06 12:18:18

标签: python python-3.x

我有这样的代码选择:

def obsfucate_letters(letters):

    letters[0] = obsfucate_function()
    letters[1] = obsfucate_function()
    letters[2] = obsfucate_function()
    letters[3] = obsfucate_function()

    return letters

def obsfucate_function():

    import random as r
    randomnumber = r.random() * 100
    if randomnumber <= 16:
        letters1 = "!"
        return letters1
    elif randomnumber > 16 and randomnumber <= 30:
        letters1 = "%"
        return letters1
    elif randomnumber > 30 and randomnumber <= 45:
        letters1 = "&"
        return letters1
    elif randomnumber > 45 and randomnumber <= 60:
        letters1 = "*"
        return letters1
    elif randomnumber > 60 and randomnumber <= 75:
        letters1 = "#"
        return letters1
    elif randomnumber > 75 and randomnumber <= 90:
        letters1 = "@"
        return letters1

现在,如果值随机数超过90,它将自动赋予值None并将letters[0]替换为None

但我想要做的是,如果数字在90到100之间,我希望它显示原始值。所以最初存储在letters[0]中的内容。

真的不知道该怎么做。希望我能够清楚地解释清楚。

3 个答案:

答案 0 :(得分:0)

您可以改为引发异常,然后通过捕获该异常来阻止letters[0]被替换:

def obsfucate_letters(letters):
    try:    
        letters[0] = obsfucate_function()
    except ValueError:
        # ignore the exception, letters[0] will be unchanged
        pass

    try:
        letters[1] = obsfucate_function()
    except ValueError:
        pass

    try:
        letters[2] = obsfucate_function()
    except ValueError:
        pass

    try:
        letters[3] = obsfucate_function()
    except ValueError:
        pass

    return letters

def obsfucate_function():
    import random as r
    randomnumber = r.random() * 100
    if randomnumber <= 16:
        letters1 = "!"
        return letters1
    elif randomnumber > 16 and randomnumber <= 30:
        letters1 = "%"
        return letters1
    elif randomnumber > 30 and randomnumber <= 45:
        letters1 = "&"
        return letters1
    elif randomnumber > 45 and randomnumber <= 60:
        letters1 = "*"
        return letters1
    elif randomnumber > 60 and randomnumber <= 75:
        letters1 = "#"
        return letters1
    elif randomnumber > 75 and randomnumber <= 90:
        letters1 = "@"
        return letters1
    else:
        raise ValueError('random number out of range')

你也可以将当前值传递给obfuscate_function()函数,如果随机值大于90则返回:

def obsfucate_letters(letters):
    letters[0] = obsfucate_function(letters[0])
    letters[1] = obsfucate_function(letters[1])
    letters[2] = obsfucate_function(letters[2])
    letters[3] = obsfucate_function(letters[3])

    return letters

def obsfucate_function(default):
    import random as r
    randomnumber = r.random() * 100
    if randomnumber <= 16:
        letters1 = "!"
        return letters1
    elif randomnumber > 16 and randomnumber <= 30:
        letters1 = "%"
        return letters1
    elif randomnumber > 30 and randomnumber <= 45:
        letters1 = "&"
        return letters1
    elif randomnumber > 45 and randomnumber <= 60:
        letters1 = "*"
        return letters1
    elif randomnumber > 60 and randomnumber <= 75:
        letters1 = "#"
        return letters1
    elif randomnumber > 75 and randomnumber <= 90:
        letters1 = "@"
        return letters1
    else:
        return default

你可以尝试避免重复自己;例如在obfuscate_letters中使用循环:

def obsfucate_letters(letters):
    for i in range(len(letters)):
        try:
            letters[i] = obfuscate_function()
        except ValueError:
            # don't replace a letter when the random value was out of range
            pass

    return letters

或传入当前值:

def obsfucate_letters(letters):
    for i in range(len(letters)):
        letters[i] = obfuscate_function(letters[i])

    return letters

您的obsfucate_function()也可以简化;通过仅使用random.choice()进行角色选择,单独使用10%的几率进行更改,或使用bisect选择字符或基于累计权重的默认值。

使用random.choice()

import random

def obsfucate_function(default):
    if random.random() < 0.1:
        # 10% chance the default is picked
        return default
    return random.choice('!%&*#@')

使用二分法:

import random
import bisect

def obsfucate_function(default):
    cumulative_weights = [16, 30, 45, 60, 75, 90, 100]
    values = ['!', '%', '&', '*', '#', '@', default]
    return values[bisect.bisect(cumulative_weights, random.randrange(100))]

random.choice()选项并不完全复制原始函数,因为选择!的机会是16%,而其余字符有15%的机会被选中。二分法将选择具有完全相同概率的字符。

答案 1 :(得分:0)

它没有返回,因为你的值没有返回值。换句话说,你没有为90到100之间的范围定义最终的elif条件

答案 2 :(得分:0)

最简单的方法是使用letters[i] = obsfucate_function() or letters[i]。当obsfucate_function返回None(randomnumber> 90)时,它将自动使用字母[i]的值。

顺便说一句,你可能想要使用混淆而不是黑曜石。