从python中的选定键打印随机值

时间:2016-08-26 01:17:20

标签: python-2.6

我想从所选键中打印一个随机值。代码内部是解释代码的注释。

cases = {
'wildfire' : {
    'blue' : ['1', '2', '3', '4', '5'],
    'purple' : ['6', '7', '8', '9', '10'],
    'pink' : ['11', '12', '13', '14', '15'],
    'red' : ['16', '17', '18', '19', '20'],
    'knives' : ['k', 'b', 'f']
    },
'phoenix' : {
    'blue' : ['1', '2', '3', '4', '5'],
    'purple' : ['6', '7', '8', '9', '10'],
    'pink' : ['11', '12', '13', '14', '15'],
    'red' : ['16', '17', '18', '19', '20'],
    'knives' : ['k', 'b', 'f']
    },
'gamma' : {
    'blue' : ['1', '2', '3', '4', '5'],
    'purple' : ['6', '7', '8', '9', '10'],
    'pink' : ['11', '12', '13', '14', '15'],
    'red' : ['16', '17', '18', '19', '20'],
    'knives' : ['k', 'b', 'f']
    },
'chroma' : {
    'blue' : ['1', '2', '3', '4', '5'],
    'purple' : ['6', '7', '8', '9', '10'],
    'pink' : ['11', '12', '13', '14', '15'],
    'red' : ['16', '17', '18', '19', '20'],
    'knives' : ['k', 'b', 'f']
    },
}
#First keys in dictionary are cases which can be selected by user
#The keys in cases dictionary are scaled from common to uncommon (top to               bottom)
#Values in the cases dictionary are the skins.
case_keys = 10
#case_keys are used to open cases
while case_keys >0:
resp=raw_input("Which case would you like to open? ")
for i in cases:
    if resp == i:
        chance = random.randint(1, 100)
        """HELP HERE. The skins are classed by rarity. E.g blue is common
but purple is more rare than blue and so forth. E.g blue is assigned to 25,
purple to 17, pink to 10, red to 5, knives to 1. E.g 45(chance) >= x,   output:blue is chosen, and from its list a random skin is selected."""

输出应该是例如:8

我正在使用python 2.6。不幸的是,我无法升级。

1 个答案:

答案 0 :(得分:0)

可能会在这里走出困境,但也许这会有所帮助

import random

cases = {
     'wildfire' : ['1', '2', '3', '4', '5'],
     'phoenix' : ['6', '7', '8', '9', '10'],
     'gamma' : ['11', '12', '13', '14', '15'],
     'chroma' : ['16', '17', '18', '19', '20'],
    }


user_input = random.choice(cases.keys())
# user_input = one of 'wildfire', 'phoenix', 'gamma', or 'chroma'
index = random.randint(0, len(cases[user_input]))
# index = random integer between 0 and 4 used to index cases

chance = random.randint(1, 100)

for i, n in enumerate([35, 17, 5, 2]):
    if chance >= n:
        print "You've won a %s skin." % cases[user_input][index] + \
              " With a chance of, %s" % chance
        break

运行此代码段的示例输出:

You've won a 15 skin. With a chance of, 84
You've won a 8 skin. With a chance of, 88
You've won a 20 skin. With a chance of, 76