Unwanted Brackets Output in Rubik's Cube Scrambler

53 Views Asked by At

I am making a Rubik's cube scrambler program using python 3.10.2 and everything works correctly except for one thing. When the randomly generated scramble is outputted, unwanted square brackets, quotation marks and commas show up as well. Any idea on how to fix this? -Thank you

import random

notation = ["R","L","F","B","U","D","R'","L'","F'","B'","U'","D'","R2","L2","F2","B2","U2","D2"]
scramble = []
i = 0

while i < 9:
    randomNotation = random.choice(notation)
    scramble.append(randomNotation)
    i += 1
print(scramble)

Output: ['L2', 'L', "R'", "D'", 'L', 'F2', 'R', "D2", 'L']

2

There are 2 best solutions below

0
On

You're printing a list and that's what the default string representation of list looks like. If you want to format the values into something different you have many options. For example:

> print(''.join(scramble))
D2FB2L'R2LDF'U

> print(' '.join(scramble))
D2 F B2 L' R2 L D F' U

> for step, rotation in enumerate(scramble, 1):
>     print(f'{step}: {rotation}')
1: D2
2: F
3: B2
4: L'
5: R2
6: L
7: D
8: F'
9: U
0
On

Your input needs to be scrubbed a bit more. For example, one of your elements is "D'" with an extra single quotation mark. This should solve your problem.

stringList = ['L2', 'L', "R", "D", 'L', 'F2', 'R', "D2", 'L']
totalString = ""
for element in stringList:
  totalString += element
print (totalString) # Prints L2LRDLF2RD2L