2017-08-07 75 views
0

我正在編寫一個類似yahtzee的遊戲,其中玩家擲5個骰子並選擇重擲哪個骰子。Python:在列表中迭代true和false變量,結果不同

我不能讓我的函數正確迭代用戶輸入驗證它們是有效的。

下面是一些代碼:

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功能,這樣我可以只輸入真值,它工作正常,我只能輸入錯誤的值,並獲得唯一想要的效果第一個錯誤的值(我知道基於代碼...但希望改變),或者我可以輸入true和false值,但它只會影響真實值,但會忽略錯誤值。

如何迭代這些值並重新提示用戶輸入所有真值?

+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