python-3.xspacing

Why is there occasionally an additional space in the output of printing the screen? I can't find it anywhere. (Python 3 Spacing Issue)


When calling the printscreen() function, the output occasionally adds a space (assumedly print('')) in between the set of printed resources and the depiction of items. This occasionally pushes the top text out of the frame. The main issue, however, is that it doesn't happen consistently, and I can't find any reason as to why.

Full Code Here

The function printscreen() is called at the start of each loop of the main game loop.

def printscreen():
    refreshscreen()
    screenrendersize = 6
    
    if UCopperScout == True:
        print("[Mine(", ScoutValue, "): '", IPickInput,"'] [Woodcut: '", IAxeInput, "'] [Scout: '", IScoutInput, "']   { Upgrades: '&' } { Trading: '$' }", sep='')
        print("_____________________________________________   ----------------------------------")
    elif UTradingPost == True:
        print("[Mine: '", IPickInput,"'] [Woodcut: '", IAxeInput, "']   { Upgrades: '&' } { Trading: '$' }", sep='')
        print("____________________________   ----------------------------------")
    elif UUnlockAxes == True:
        print("[Mine: '", IPickInput, "'] [Woodcut: '", IAxeInput, "']   { Upgrades: '&' }", sep='')
        print("_____________________________  -----------------")
    else:
        print("[Mine: '", IPickInput, "']   { Upgrades: '&' }", sep='')
        print("____________   -----------------")
    
    if PCopper >= 1:
        print("[ Stone:", PStone, "] [ Wood:", PWood, "] [ Feather:", PFeather, "] [ Coal:", PCoal, "] [ Copper:", PCopper, "]")
    elif PCoal >= 1:
        print("[ Stone:", PStone, "] [ Wood:", PWood, "] [ Feather:", PFeather, "] [ Coal:", PCoal, "]")
    elif PFeather >= 1:
        print("[ Stone:", PStone, "] [ Wood:", PWood, "] [ Feather:", PFeather, "]")
    elif PWood >= 1:
        print("[ Stone:", PStone, "] [ Wood:", PWood, "]")
    else:
        print("[ Stone:", PStone, "]")
    
    if PCell >= 1:
        print("{ Cell:", PCell, "}")
        screenrendersize -= 1
    
    if UCopperRod >= 1:
        print("0==) x", URollingLog, "   ", ISnareDisplay, "x", UBirdSnare, "    (_) x", UBeggarHat, "    _i_ x", UCopperRod)
        screenrendersize -= 1
    elif UBeggarHat >= 1:
        print("0==) x", URollingLog, "   ", ISnareDisplay, "x", UBirdSnare, "    (_) x", UBeggarHat)
        screenrendersize -= 1
    elif UBirdSnare >= 1:
        print("0==) x", URollingLog, "   ", ISnareDisplay, "x", UBirdSnare)
        screenrendersize -= 1
    elif URollingLog >= 1:
        print("0==) x", URollingLog)
        screenrendersize -= 1
    
    if UForge == True:
        print("'", IForgeDisplay, "' ", UForgeStatus, sep='')
        screenrendersize -= 1
    
    for i in range(0, screenrendersize):
        print("")

The output is mostly fine, giving what is expected (i.e.):

[Mine(0): '-['] [Woodcut: '[`'] [Scout: oo']   { Upgrades: '&' } { Trading: '$' }
____________________________________________   ----------------------------------
[ Stone: 116] [ Wood: 98 ] [ Feather: 23 ] [ Coal: 50 ] [ Copper: 9 ]
0==) x3     #_# x 10     (_) x 4     _i_ x 2
'(=)' Inactive




But occasionally prints as:

[Mine(0): '-['] [Woodcut: '[`'] [Scout: oo']   { Upgrades: '&' } { Trading: '$' }
____________________________________________   ----------------------------------
[ Stone: 116] [ Wood: 98 ] [ Feather: 23 ] [ Coal: 50 ] [ Copper: 9 ]

0==) x3     #_# x 10     (_) x 4     _i_ x 2
'(=)' Inactive




Which accordingly shoves the top text out of the frame.

In attempting to solve the issue, I have looked over the affected area multiple times, gotten printouts for each space made by screenrendersize, for which it does not appear, looked for similar issues online, none of which I could find, and have switched variables. I'm ready to accept it as a flaw of Python 3, but I just want to make sure first.

Thank you in advance!

ANSWER: Adding more sep='' to text and adding a space (I did a double-space, but a space could work too) to the end of each string has appeared to solve the issue. Thank you so much!


Solution

  • edit: I just realized you already set sep='' in some places, but not all, hence your issue.

    From the docs (emphasis mine):

    print(*objects, sep=' ', end='\n', file=None, flush=False)

    Print objects to the text stream file, separated by sep and followed by end. sep, end, file, and flush, if present, must be given as keyword arguments.

    So print will put the sep between each object passed to objects. The default value of sep is a space (' '), which explains what you're seeing.

    You can either set the sep to an empty string (''), or more typically, just build up the string yourself, e.g.:

    print(f"[Mine({ScoutValue}): '{IPickInput}'] [Woodcut: '{IAxeInput}'] [Scout: '{IScoutInput}']   { Upgrades: '&' } { Trading: '$' }")