简体   繁体   English

Rubik's Cube Scrambler 中不需要的括号 Output

[英]Unwanted Brackets Output in Rubik's Cube Scrambler

I am making a Rubik's cube scrambler program using python 3.10.2 and everything works correctly except for one thing.我正在使用 python 3.10.2 制作魔方加扰器程序,除一件事外,一切正常。 When the randomly generated scramble is outputted, unwanted square brackets, quotation marks and commas show up as well.当输出随机生成的 scramble 时,不需要的方括号、引号和逗号也会出现。 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'] Output: ['L2', 'L', "R'", "D'", 'L', 'F2', 'R', "D2", 'L']

You're printing a list and that's what the default string representation of list looks like.您正在打印一个list ,这就是list的默认字符串表示形式。 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

Your input needs to be scrubbed a bit more.您的输入需要再擦洗一下。 For example, one of your elements is "D'" with an extra single quotation mark.例如,您的元素之一是带有额外单引号的“D'”。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM