在Python中打印随机选择的变量

时间:2013-03-12 01:02:09

标签: python python-2.7

我正在为Heroscape游戏制作一个骰子(因此我们可以与离岸朋友一起玩)。 heroscape的骰子有6个方面。 3面显示头骨,1面有标志,2面有盾牌。

我已经让它随机生成其中的一面,但我希望它能在最后列出结果(即你掷了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语句,但我意识到如果我尝试打印('diceRoll'),我得到的就是整个数组而不是随机选择的骰子卷。

我不确定如何保存每个diceRoll,所以我可以在以后打印这个号码。

(我的想法就像是

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

2 个答案:

答案 0 :(得分: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随机滚动结果。如果您还没有了解过生成器表达式,我建议您查看它们,因为它们非常方便。

答案 1 :(得分: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.')
相关问题