1

我正在为游戏 Heroscape 做一个掷骰子(所以我们可以和离岸的朋友一起玩)。heroscape 的骰子有 6 个面。3 面显示头骨,1 面有符号,2 面有盾牌。

我已经让它随机生成其中的 1 个,但我希望它在最后列出结果(即你滚动了 6 个头骨、2 个符号和 4 个盾牌)。

这是我当前的代码:

loop = 1
while loop == 1:
    diceChoose = raw_input('Pick your dice. (Press 1 for a D20 roll, and 2 for attack /defense dice.) ')
    if diceChoose == ('1'):
        import random
        for x in range(1):
            print random.randint(1,21),
            print
        raw_input("YOUR DICE ROLL(S) HAVE COMPLETED. PRESS ANY KEY TO CONTINUE.")
    elif diceChoose == ('2'):
        diceNo = int(raw_input('How many dice do you need? '))
        testvar = 0
        diceRoll = ['skull', 'skull', 'skull', 'symbol', 'shield', 'shield']
        from random import choice

        while testvar != diceNo:
            print choice(diceRoll)
            testvar = testvar + 1
            if testvar == diceNo:
                print ('YOUR DICE ROLLS HAVE COMPLETED')

        raw_input("PRESS ANY KEY TO CONTINUE.")

    else: loop = raw_input('Type 1 or 2. Nothing else will work. Press 1 to start the program again.')

我尝试的是一堆 if 语句,但我已经意识到,如果我尝试 print ('diceRoll'),我得到的只是整个数组,而不是随机选择的骰子。

我不确定如何在每个 diceRoll 发生时保存它,因此我可以稍后打印该数字。

(我的想法是

if diceRoll == 'skull' skullNo +1 
print('skullNo'))
4

2 回答 2

0

我认为你可以使用标准库中Counter的模块来解决这个问题的一个好的数据结构。collections它有点像字典,将对象映射到整数。但是,您可以为其添加值,这将增加它们的计数。

所以,我会这样做:

from random import choice
from collections import Counter

diceNo = int(raw_input('How many dice do you need? '))
diceValues = ['skull', 'skull', 'skull', 'symbol', 'shield', 'shield']

counter = Counter()
counter.update(choice(diceValues) for _ in range(diceNo))

print("Rolls:")
for value, count in counter.items():
    print("{}: {}".format(value, count))

print ('YOUR DICE ROLLS HAVE COMPLETED')
raw_input("PRESS ANY KEY TO CONTINUE.")

关键线是counter.update(choice(diceValues) for _ in range(diceNo))。这使用“生成器表达式”调用counter.update,生成diceNo随机滚动结果。如果您还没有了解生成器表达式,我建议您检查一下,因为它们非常方便。

于 2013-03-12T01:29:41.527 回答
0

欢迎来到 SO。我更改了您的代码的一些部分,以使它们更类似于我的风格 - 如果有帮助,请随时将它们改回来。

将循环代码更改为:

while True:

这完成了同样的事情,除了它更pythonic。

然后,在一个标签中并告诉设置数字值skull, symbolshield。在这种情况下,我们将其设置为 0。

skull = 0
symbol = 0
shield = 0

接下来,将下面的代码更改diceNo = int(raw_input('How many dice do you need? '))为此。它应该缩进一个。

    for x in xrange(diceNo):
        import random
        choice = random.randint(1,6) 

        if choice == 1:
            skull +=1
        elif choice == 2:
            skull +=1
        elif choice == 3:
            skull +=1
        elif choice == 4:
            symbol =+ 1
        elif choice == 5:
            shield += 1
        elif choice == 6:
            shield += 1

此代码重复所需的骰子数量。然后,我将 1 添加到与该名称关联的变量中。

在其正下方,我们向用户显示信息。

print "You rolled %d skulls, %d symbols, and %d shields. Congrats." % (skull, symbol, shield)

由于您似乎对代码有疑问,因此我决定将其完整发布。

while True:

    #Here we set the variables

    skull = 0
    symbol = 0
    shield = 0

    diceChoose = raw_input('Pick your dice. (Press 1 for a D20 roll, and 2 for attack /   defense dice.) ')
    if diceChoose == '1':
        import random
        for x in range(1):
            print random.randint(1,21),
            print
        raw_input("YOUR DICE ROLL(S) HAVE COMPLETED. PRESS ANY KEY TO CONTINUE.")
    elif diceChoose == '2':
        diceNo = int(raw_input('How many dice do you need? '))

        for x in xrange(diceNo):
            import random
            choice = random.randint(1,6) 

            if choice == 1:
                skull +=1
            elif choice == 2:
                skull +=1
            elif choice == 3:
                skull +=1
            elif choice == 4:
                symbol =+ 1
            elif choice == 5:
                shield += 1
            elif choice == 6:
                shield += 1

        print "You rolled %d skulls, %d symbols, and %d shields. Congrats." % (skull, symbol, shield)

        raw_input("PRESS ANY KEY TO CONTINUE.")

    else: loop = raw_input('Type 1 or 2. Nothing else will work. Press 1 to start the program again.')
于 2013-03-12T01:31:42.183 回答