0

dice is a variable in a function not included in this code but it generates a list of 5 random numbers between 1 and 6. the function below i am trying to implement so that the user can choose which index in the list they want to change using an input. and taking the input from the user it changes those index's in the list with new random numbers between 1 and 6. ive tried it below but it is a very lengthy process, is there another way?

def reroll(dice):
    rollagain = str(input("Which dice would you like to re-roll, input in ascending order (options are: 1,2,3,4,5): "))
    if rollagain == '1,2,3,4,5':
        dice[0, 1, 2, 3, 4].append = [randint(1, 6)]
        return True
    elif rollagain == '1,2,3,4':
        dice[0, 1, 2, 3].append = [randint(1, 6)]
        return True
    elif rollagain == '1,2,3':
        dice[0, 1, 2].append = [randint(1, 6)]
        return True
    elif rollagain == '1,2':
        dice[0, 1].append = [randint(1, 6)]
        return True
    elif rollagain == '1':
        dice[0].append = [randint(1, 6)]
        return True
    elif rollagain == '1,3,5':
        dice[0, 2, 4].append = [randint(1, 6)]
        return True
    elif rollagain == '2, 4':
        dice[1, 3].append = [randint(1, 6)]
        return True
    elif rollagain == '2':
        dice[1].append = [randint(1, 6)]
        return True
    elif rollagain == '3':
        dice[2].append = [randint(1, 6)]
        return True
    elif rollagain == '4':
        dice[3].append = [randint(1, 6)]
        return True
    elif rollagain == '5':
        dice[4].append = [randint(1, 6)]
        return True
    elif rollagain == '1,4,5':
        dice[0, 3, 4].append = [randint(1, 6)]
        return True
    elif rollagain == '1,2,5':
        dice[0, 1, 4].append = [randint(1, 6)]
        return True
    else:
        return False
karthikr
  • 97,368
  • 26
  • 197
  • 188
BinaryBoy
  • 1
  • 3

3 Answers3

0

Try splitting "rollagain" to a tuple you then pass to "dice". That should shorten it a bit :-)

MeetTitan
  • 3,383
  • 1
  • 13
  • 26
0

I think I understand what you're trying to do. (assuming dice is a list, if it isn't, just change the assignment)

Try this:

def reroll(dice):
    rollagain = str(input("Which dice would you like to re-roll, input in ascending order (options are: 1,2,3,4,5): "))
    for combo in [[0,1,2,3,4],[0,1,2,3],[0,1,2],[0,1],[0],[0,2,4],[1,3],[1],[2],[3],[4],[0,3,4],[0,1,4]]:
        if rollagain == ','.join(map(lambda x: str(x+1), combo)):
            for index in combo:
                dice[index] = randint(1, 6)
            return True
    return False
Οurous
  • 348
  • 6
  • 17
0

The code below will only work in Python 3.x, which you seem to be using. If you are using Python 2.x, the input built-in has a different behavior.

What I understood is that the user can select which of the indexes of the original dice must be modified, right? If that's the case, and since input is clever enough to evaluate 2,3 to a tuple of integers (2,3), you can have your code as follows:

rollagain = input("Which dice would you like to re-roll,"
                  " input in ascending order (options are: 1,2,3,4,5): ")
indexes_to_change = [int(index) for index in rollagain.split(',')]
print "User wants to change: %s" % (indexes_to_change)
print "Before change, the dice list is: %s" % dice
for index_to_change in indexes_to_change:
    dice[index_to_change-1] = random.randint(1, 6)
print "After change, the dice list is: %s" % dice

I've added some print statements that might help understand what's going on. You should also take a look to the split method of Python strings and how to convert an string containing a numeric value to an actual int (see this SO thread)

You should read about Exceptions as well... You might need them if the user decides to enter "foo" instead of some comma separated numbers.

Also, if you're using Python < 3, contrary to what I thought, you should consider using raw_input instead of input, because input (in Python < 3) uses eval and eval is evil.

Community
  • 1
  • 1
Savir
  • 17,568
  • 15
  • 82
  • 136