python-使用随机数更改列表中的特定索引

时间:2014-11-30 23:42:20

标签: python list

骰子是此代码中未包含的函数中的变量,但它生成1到6之间的5个随机数的列表。下面的函数我试图实现,以便用户可以选择列表中的哪个索引使用输入进行更改。并且从用户那里获取输入后,它会使用1到6之间的新随机数更改列表中的那些索引。我在下面尝试了它,但这是一个非常漫长的过程,还有另一种方法吗?

def reroll(dice):
    rollagain = str(input("Which dice would you like to re-roll, input in ascending order (options are: 1,2,3,4,5): "))
    if rollagain == '1,2,3,4,5':
        dice[0, 1, 2, 3, 4].append = [randint(1, 6)]
        return True
    elif rollagain == '1,2,3,4':
        dice[0, 1, 2, 3].append = [randint(1, 6)]
        return True
    elif rollagain == '1,2,3':
        dice[0, 1, 2].append = [randint(1, 6)]
        return True
    elif rollagain == '1,2':
        dice[0, 1].append = [randint(1, 6)]
        return True
    elif rollagain == '1':
        dice[0].append = [randint(1, 6)]
        return True
    elif rollagain == '1,3,5':
        dice[0, 2, 4].append = [randint(1, 6)]
        return True
    elif rollagain == '2, 4':
        dice[1, 3].append = [randint(1, 6)]
        return True
    elif rollagain == '2':
        dice[1].append = [randint(1, 6)]
        return True
    elif rollagain == '3':
        dice[2].append = [randint(1, 6)]
        return True
    elif rollagain == '4':
        dice[3].append = [randint(1, 6)]
        return True
    elif rollagain == '5':
        dice[4].append = [randint(1, 6)]
        return True
    elif rollagain == '1,4,5':
        dice[0, 3, 4].append = [randint(1, 6)]
        return True
    elif rollagain == '1,2,5':
        dice[0, 1, 4].append = [randint(1, 6)]
        return True
    else:
        return False

3 个答案:

答案 0 :(得分:0)

尝试拆分" rollagain"一个元组然后传递给"骰子"。这应该缩短一点: - )

答案 1 :(得分:0)

我想我明白你要做的事情。 (假设dice是一个列表,如果它不是,则只需更改分配)

试试这个:

def reroll(dice):
    rollagain = str(input("Which dice would you like to re-roll, input in ascending order (options are: 1,2,3,4,5): "))
    for combo in [[0,1,2,3,4],[0,1,2,3],[0,1,2],[0,1],[0],[0,2,4],[1,3],[1],[2],[3],[4],[0,3,4],[0,1,4]]:
        if rollagain == ','.join(map(lambda x: str(x+1), combo)):
            for index in combo:
                dice[index] = randint(1, 6)
            return True
    return False

答案 2 :(得分:0)

以下代码仅适用于您似乎正在使用的Python 3.x.如果您使用的是Python 2.x,则input内置函数会有不同的行为。

我的理解是用户可以选择必须修改原始dice的哪个索引,对吧?如果是这种情况,并且由于input足够聪明,可以将2,3评估为整数元组(2,3),您可以按如下方式获取代码:

rollagain = input("Which dice would you like to re-roll,"
                  " input in ascending order (options are: 1,2,3,4,5): ")
indexes_to_change = [int(index) for index in rollagain.split(',')]
print "User wants to change: %s" % (indexes_to_change)
print "Before change, the dice list is: %s" % dice
for index_to_change in indexes_to_change:
    dice[index_to_change-1] = random.randint(1, 6)
print "After change, the dice list is: %s" % dice

我添加了一些print语句,可能有助于了解正在发生的事情。您还应该查看Python字符串的split方法以及如何将包含数值的字符串转换为实际的int(请参阅this SO thread

您还应该阅读Exceptions ...如果用户决定输入"foo"而不是逗号分隔的数字,则可能需要它们。

另外,如果你正在使用Python< 3,与我的想法相反,您应该考虑使用raw_input而不是input,因为input(在Python< 3中)使用evaleval is evil