简体   繁体   English

在我的代码中的适当位置添加制表符(\\ t)

[英]Adding tabs (\t) in the appropriate spot to my code

I created a code that requires tabs to put in it, but I cannot seem to figure out how to add the tabs appropriately. 我创建了一个需要在其中添加标签的代码,但似乎无法弄清楚如何正确添加标签。 See below for my code and the doc string for what it should return, and what it returns instead. 请参阅下面的代码和doc字符串,了解应返回的内容以及返回的内容。 Maybe I should rethink my whole approach? 也许我应该重新考虑整个方法?

def display_game(guesses, clues):
'''(list, list) -> str
Given two lists of single character strings, return a string 
that  displays the current state of the game   
>>>display_game([['Y', 'P', 'G', 'G'], ['O', 'O', 'G', 'G']], [['b', 'b'], ['b','b', 'b', 'b']])
'Guess\tClues\n****************\nY P G G\tb b\nO O G G\tb b b b\n'
'''
display = 'Guess\tClues\n****************\n'
for i in range(len(guesses)):
        for letter in guesses[i]:
            display += letter + ' '
        for letter in clues[i]:
            display += letter + ' '
        display += '\n'
return display

When I use it (using the doc string example), I get: 当我使用它(使用doc字符串示例)时,我得到:

display_game([['Y', 'P', 'G', 'G'], ['O', 'O', 'G', 'G']], [['b', 'b'],      ['b','b', 'b', 'b']])

'Guess\\tClues\\n****************\\nY PGG bb \\nO OGG bbbb \\n' '猜\\ t线索\\ n **************** \\ nY PGG bb \\ nO OGG bbbb \\ n'

Any attempt to put \\t in the code has it turning out wrong (ex: with \\t between each string instead of where they should be as per the doc string). 任何在代码中加上\\ t的尝试都会导致错误(例如:在每个字符串之间使用\\ t而不是在文档字符串中应使用的位置)。 Is anyone able to suggest how I may change things around? 有人能建议我如何改变周围的事物吗? Thanks! 谢谢!

Your code does not add a tab in between the guess and the clue. 您的代码不会在猜测和线索之间添加标签。 You could simply add 您可以简单地添加

display += '\\t'

in between the first and second nested for loops, however, you then need to ensure that a trailing space is not added at the end of the first loop. 但是,在第一个和第二个嵌套的for循环之间,则需要确保在第一个循环的末尾没有添加尾随空格。

str.join() is a better way to handle this as it only adds delimiter strings in between the items of a sequence: str.join()是处理此问题的更好方法,因为它仅在序列的项目之间添加定界符字符串:

>>> ' '.join(['a', 'b', 'c'])
'a b c'

Notice that there is no trailing space character in the above. 请注意,上面没有尾随空格字符。 Applying that to your function: 将其应用于您的功能:

def display_game(guesses, clues):
    display = 'Guess\tClues\n****************\n'
    for guess, clue in zip(guesses, clues):
        display += '{}\t{}\n'.format(' '.join(guess), ' '.join(clue))
    return display

zip() is also used here to pair each guess and clue. zip()在这里也用于配对每个猜测和线索。 Then it's simply a matter of using str.join() on the guess and clue, and building the string with the tab in the required place. 然后,只需在猜测和线索上使用str.join() ,然后在所需位置使用制表符构建字符串即可。

>>> assert(display_game([['Y', 'P', 'G', 'G'], ['O', 'O', 'G', 'G']], [['b', 'b'], ['b','b', 'b', 'b']]) == 'Guess\tClues\n****************\nY P G G\tb b\nO O G G\tb b b b\n')

You can just add it in between the for loops: 您可以将其添加到for循环之间:

for i in range(len(guesses)):
        for letter in guesses[i]:
            display += letter + ' '
        display += '\t' # right here
        for letter in clues[i]:
            display += letter + ' '
        display += '\n'
return display

This worked for me. 这对我有用。 Just add the tab between those two for loops of guesses and clues. 只需在这两个之间添加制表符即可获得猜测和线索循环。

def display_game(guesses, clues):
    display = 'Guess \t Clues \n **************** \n'
    for i in range(len(guesses)):
        for letter in guesses[i]:
            display += letter + ' '
        display += '\t'
        for letter in clues[i]:
            display += letter + ' '
        display += '\n'
    return display

print(display_game('at', 'yk'))

This gave output: 这给出了输出:

Guess    Clues 
**************** 
a        y 
t        k 

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

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