从 python 中的选定键打印随机值
Print random value from selected key in python
我想从选定的键中打印一个随机值。代码里面是解释代码的注释。
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。不幸的是,我无法升级。
这里可能有点冒险,但也许这会有所帮助
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
我想从选定的键中打印一个随机值。代码里面是解释代码的注释。
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。不幸的是,我无法升级。
这里可能有点冒险,但也许这会有所帮助
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