0
votes

I'm programming a yahtzee like game where a player rolls 5 dice and gets to pick which dice to re-roll.

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.

How can I iterate over these values and re-prompt the user to enter all true values?

1
What is the "desired effect"? - Carcigenicate
@Carcigenicate I apologize for being unclear, I'll update the original post. I want it to verify that all values from the user match values that were actually in the original 5 dice rolled. If they are not, I would like it to go back to prompting them to enter valid values. This is the behavior that takes place when I enter only false values, however, it only tells the user 'I don't think you rolled x' for only the first false value entered. - Hanzy
Is there any reason that you're learning Python with Python2? These days you should be using Python3 - Wayne Werner
@WayneWerner I suppose I should switch to Python3; I was referred to "Learn Python the Hard Way," which I found out uses Python2. But I would like to get a new book for Python3. I welcome any suggestions for a good resource. I like more structure than LPTHW, but getting my hands dirty has been good. - Hanzy
Yeah, using Python2 is one of the many problems with that book. We have a nice list of better resources - Wayne Werner

1 Answers

0
votes

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.