dice是一个函数中的一个变量,它不包含在这个代码中,但是它会生成一个5个介于1和6之间的随机数的列表。下面的函数是我试图实现的,这样用户就可以使用输入来选择要更改列表中的哪个索引。从用户那里输入数据,它会用1到6之间的新随机数改变列表中的索引。我在下面试过了,但这是一个非常漫长的过程,有没有其他方法?在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