Recently, I watched a video about a riddle named Cheat Death (watch it check out the channel too!). So I thought about a python code to solve it. It works and solved the riddle too (Not relevant to the given code). I also wrote a code which prints every 5 dice roll sequence and the final position of the player in the snake-ladder board.
fr=[4,5,19,21,28,35,44,47,52,53,59,70,76,81,88,98] #Snakes and ladders start positions. to=[75,15,41,3,50,96,82,30,23,94,95,91,41,62,67,12] #Snakes and ladders end positons. def jump(n): #Returns position after each roll. if n in fr: return to[fr.index(n)] else: return n pos=[1,0,0,0,0,0] #Position after each roll (pos is the initial position). for a in range(1,7): #a,b,c,d,e is the dice value of each roll. pos=jump(pos+a)#pos[i] is the position after ith roll. for b in range(1,7): pos=jump(pos+b) for c in range(1,7): pos=jump(pos+c) for d in range(1,7): pos=jump(pos+d) for e in range(1,7): pos=jump(pos+e) print(a,b,c,d,e," ",pos,end="") #(Dice value in each roll, Final position) print(end=" ") #For neat printing. if pos<=9: print(end=" ") #For neat printing.
I know nested for loop isn’t the best way to do it. So, can you please help me to make this code more efficient.