2017-04-19 10 views
0

私はD & Dスタイルのキャラクタージェネレーターを作っています。私はそれらのための統計をロールして、彼らが望む能力スコアにそれらを割り当てることを許可します。私は彼らが同じセクションで再び同じセクションで戦っていたからスタートする能力を持っていたいと思っています。ここでリストと複数の入力を試してみよう

は私が

from random import randint 
def char_stats(): 
    # roll 4 D6s drop the lowest number and add the highest 3 
    s1,s2,s3,s4,s5,s6 = ([],[],[],[],[],[]) 
    for x in range(4): 
     s1.append(randint(1,6)) 
     s2.append(randint(1,6)) 
     s3.append(randint(1,6)) 
     s4.append(randint(1,6)) 
     s5.append(randint(1,6)) 
     s6.append(randint(1,6)) 
    stat1 = sorted(s1) 
    stat2 = sorted(s2) 
    stat3 = sorted(s3) 
    stat4 = sorted(s4) 
    stat5 = sorted(s5) 
    stat6 = sorted(s6) 
    return sum(stat1[1:]),sum(stat2[1:]),sum(stat3[1:]),sum(stat4[1:]),sum(stat5[1:]),sum(stat6[1:]) 

a = list(char_stats()) 
print "Please choose one of the following for your stat: {}".format(a) 
while len(a) > 0: 
    try: 
     Strength = int(raw_input('Please input one of these stats for your Strength:\n')) 
     if Strength in a: 
      a.remove(Strength) 
      print a 
     Wisdom = int(raw_input('Please input one of these stats for your Wisdom:\n')) 
     if Wisdom in a: 
      a.remove(Wisdom) 
      print a 
     Intelligence = int(raw_input('Please input one of these stats for your Intelligence:\n')) 
     if Intelligence in a: 
      a.remove(Intelligence) 
      print a 
     Constitution = int(raw_input('Please input one of these stats for your Constitution:\n')) 
     if Strength in a: 
      a.remove(Constitution) 
      print a 
     Dexterity = int(raw_input('Please input one of these stats for your Dexterity:\n')) 
     if Dexterity in a: 
      a.remove(Dexterity) 
      print a 
     Charisma = int(raw_input('Please input one of these stats for your Charisma:\n')) 
     if Charisma in a: 
      a.remove(Charisma) 
    except ValueError: 
     print "Incorrect Input" 
     continue 

持っているものである私は(私は非常に悪い形であると考えている)if文のそれぞれをネストしようとしたと同様の結果を持っています。私はまた、すべての入力をtryではなく、計算にグループ化してみて、同じ結果を得ました。何かアドバイス?

答えて

1

あなたが入力の形式(int型)、および値の両方のためのロジック(それが巻か統計情報の一覧にある「有効になるまでループ」を使用する必要がありますか? )。基本的なロジックはこれです:あなたのケースでは

while True: 
    # get input 
    # check input 
    # if input is valid, 
    # break 

、これは今

while True: 
    user = input("Please enter a stat to use") 
    if user.isnumeric(): 
     stat_choice = int(user) 
     if stat_choice in a: 
      break 

ようになり、これの有効に利用するために、あなたは6つの統計情報をパラメータとにそれらを配置する必要がありますループ:あなたは同様にいくつかのラインにあなたのchar_statsルーチンを短縮することができます

stat_name = ["Strength", "Wisdom", ...] 
player_stat = [0, 0, 0, 0, 0, 0] 

for stat_num in range(len(player_stat)): 
    while True: 
     user = input("Please input one of these stats for your" + \ 
         stat_name[stat_num] + ": ") 
     # Validate input as above 

    player_stat[stat_num] = stat_choice 

注意s。

あなたは動いていますか?

+0

私は少し動いてしまいます。私はあなたが行っている論理を見ています。私はもう少し詳しく説明します。 –

+0

素晴らしい!楽しむ! – Prune

0

あなたのコードに少なくとも1つの機能があります(char_stats)ので、どのように機能するか知っているように感じています。

これは機能を使用するのに最適な場所です。

このコードでは、try/except、質問の質問、統計リストのチェックを組み込んだ関数を記述することをお勧めします。このような

何か:

def pick_a_stat(stats, prompt): 
    """Prompt the user to pick one of the values in the `stats` list, 
    and return the chosen value. Keep prompting until a valid entry 
    is made. 
    """ 

    pass # Your code goes here. 
関連する問題