2017-08-07 87 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