2017-08-07 4 views
0

私は、プレイヤーが5つのサイコロをロールし、どのサイコロをリロールするかを選ぶような、ゲームのようなyahtzeeをプログラミングしています。Python:異なる結果を持つリスト内の真と偽の変数を繰り返します

ユーザー入力を適切に反復処理して、関数が有効であることを確認できません。

ここにいくつかのコードです:

def diceroll(): 
    raw_input("Press enter to roll dice: ") 
    a = random.randint(1, 6) 
    b = random.randint(1, 6) 
    c = random.randint(1, 6) 
    d = random.randint(1, 6) 
    e = random.randint(1, 6) 
    myroll.append(a) 
    myroll.append(b) 
    myroll.append(c) 
    myroll.append(d) 
    myroll.append(e) 
    print "Your roll:" 
    print myroll 
    diceSelect() 

def diceSelect(): 
    s = raw_input("Enter the numbers of the dice you'd like to roll again, separated by spaces, then press ENTER: ")  
    rollAgain = map(int, s.split()) 
    updateMyRoll(rollAgain) 

def updateMyRoll(a): 
    reroll = [] 
    for n in a: 
     if n in myroll: 
      reroll.append(n) 
      removeCommonElements(myroll, a) 
      print "deleting elements..." 
     elif n not in myroll: 
      print "I don't think you rolled", n, "." 
      diceSelect() 
     else: 
      print "I don't understand..." 
      diceSelect() 
     print "Your remaining dice: ", myroll 

def removeCommonElements(a,b,): 
for e in a[:]: 
    if e in b: 
     a.remove(e) 
     b.remove(e) 

問題がdiceSelect機能ではそうです、私は唯一の真の値を入力することができますし、それが正常に動作するように、私は偽の値を入力するだけのために所望の効果を得ることができますが、最初の偽値(私はコードに基づいて理解していますが、変更したいと思います)、または真と偽の値を入力できますが、真の値に対してのみ作用しますが、偽の値は無視します。

どのようにこれらの値を反復処理して、すべての真の値を入力するようにユーザーに促すことができますか?

+0

「希望の効果」とは何ですか? – Carcigenicate

+0

@Carcigenicate私は不明であることをお詫びします、私は元のポストを更新します。ユーザーのすべての値が実際に元の5サイコロにあった値と一致していることを確認したいと思います。そうでない場合は、有効な値を入力するように促すように戻してください。これは、偽の値のみを入力したときに発生する動作ですが、最初に入力した誤った値のみについてユーザーに「xを押したとは思わない」と伝えます。 – Hanzy

+1

Python2でPythonを学習する理由はありますか?最近はPython3を使うべきです –

答えて

0

あなたのコードにはいくつかの問題があります。私はあなたのコードを少し書き直しました:

def diceroll(dice_count=6): 
    raw_input("Press enter to roll dice: ") 
    # No need to create a variable for each roll. 
    # Also modifying global variables is a bad idea 
    rolls = [] 
    for _ in range(dice_count-1): 
     rolls.append(random.randint(1,6)) 
    # or **instead** of the above three lines, a list 
    # comprehension 
    rolls = [random.randint(1,6) for _ in range(dice_count-1)] 
    return rolls 

def roll_select(): 
    # one letter variable names are hard to follow 
    choices = raw_input("Enter the numbers of the dice you'd like to roll again, separated by spaces, then press ENTER: ")  
    # again, modifying global variables is a bad idea 
    # so return the selection 
    return map(int, choices.split()) 

def roll_new_dice(myroll): 
    # no need to create a new list, we have everything 
    # we need right here 
    for val in roll_select(): 
     try: 
      print('deleting {}'.format(val)) 
      # we can just remove the values directly. We'll get 
      # an exception if they're not in the list. 
      myroll.remove(val) 
     except ValueError: 
      print("That wasn't one of your rolls") 
    # Then we can re-use our function - this time 
    # extending our list. 
    myroll.extend(diceroll(6-len(myroll))) 

rolls = diceroll() 
print('You rolled {}'.format(rolls)) 
changes = roll_select() 
if changes: 
    roll_new_dice(rolls) 
print('Your new rolls: {}'.format(rolls)) 

これは以前よりも少しはっきりしているはずです。

+0

ありがとう、これは私が求めていたほどのものでした。私は今Python3をインストールし、移行中です。私はPython3を学ぶために投稿されたリソースのいくつかを見ています。私は包括的な本を購入しても構いません。公式のPython3チュートリアルは良いですが、私のような人にとっては素晴らしい場所ではありませんが、進歩するにつれてトピックを参照するのが適切です。 – Hanzy

関連する問題