简体   繁体   中英

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. 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:

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'

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). 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.

str.join() is a better way to handle this as it only adds delimiter strings in between the items of a sequence:

>>> ' '.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. 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.

>>> 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 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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