2016-08-26 3 views
1

選択したキーからランダムな値を出力したいとします。コードの中には、コードを説明するコメントがあります。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を使用しています。残念ながら、私はアップグレードすることができません。

+0

はあなたがしようとしているものにいくつかのより多くを詳しく説明してもらえ達成するために?フォーマットされた文が印刷されているときに、出力が8になるということはどういう意味ですか?あなたは "あなたはフェニックススキンを獲得しました..."または "あなたは8個のスキンを獲得しました..."という出力を出そうとしていますか? – lanery

+0

ユーザーがケース名を入力すると、別の入力が求められます。 "開くには 'はい'を入力してください。スクリプトの残りの部分は、次のように実行されます。アウトプット:「あなたは8本のスキンを獲得しました」というのはランダムです。 –

答えて

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 
+0

新しい編集をご覧ください。答えは正しいが、より多くのコードが必要な辞書を調整した。 –

関連する問題