简体   繁体   English

Python:遍历列表中的真假变量,结果不同

[英]Python: iterate over true and false variables in a list, with different outcomes

I'm programming a yahtzee like game where a player rolls 5 dice and gets to pick which dice to re-roll. 我正在编写类似yahtzee的游戏,其中玩家掷出5个骰子,然后选择要重投的骰子。

I can't get my function to properly iterate over the user input verify that they are valid. 我无法让我的函数正确地遍历用户输入以验证它们是否有效。

Here's some code: 这是一些代码:

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)

The problem is likely in the diceSelect function, such that I can enter only true values and it works fine, I can enter only false values and get the desired effect for ONLY the first false value (which I understand based on the code... but would like to change), or I can enter true AND false values but it ONLY acts on the true values but ignores the false values. 该问题可能是在diceSelect函数中造成的,这样我只能输入真实值,并且可以正常工作,我只能输入错误值,并且仅对第一个错误值即可达到预期的效果(我根据代码了解...但我想更改),或者我可以输入“真”和“假”值,但仅作用于“真”值,而忽略了“假”值。

How can I iterate over these values and re-prompt the user to enter all true values? 我该如何遍历这些值并再次提示用户输入所有真实值?

You've got a couple of problems here in your code. 您的代码中有几个问题。 I've re-written your code a bit: 我已经重新编写了一些代码:

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))

Hopefully this should be a bit clearer than what you had before. 希望这应该比以前更清楚了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM