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

huangapple go评论55阅读模式
英文:

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

问题

当调用printscreen()函数时,输出偶尔在打印的资源集合和物品描述之间添加了一个空格(假设是print('')),这有时会将顶部文本推出框架。然而,问题是它不会一直发生,我找不到任何原因。

完整代码在这里

printscreen()函数在主游戏循环的每个循环开始时调用。

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("")

输出大部分都是正常的,符合预期(例如):

[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

但偶尔会打印如下:

[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

这会导致顶部文本被推出框架。

为了解决这个问题,我增加了更多的sep=''到文本中,并在每个字符串的末尾添加了一个空格(我做了双空格,但一个空格也可以)。谢谢!

英文:

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!

答案1

得分: 0

编辑:我刚刚意识到你已经在某些地方设置了sep='',但不是全部,因此出现了问题。

根据文档(重点是我的):

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

将对象打印到文本流文件中,由sep分隔,并以end结尾。如果存在sep、end、file和flush,必须作为关键字参数给出。

因此,print会在传递给objects的每个对象之间放置sepsep的默认值是一个空格(' '),这解释了你看到的结果。

你可以将sep设置为空字符串(''),或者更常见的做法是自己构建字符串,例如:

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

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: '$' }")

huangapple
  • 本文由 发表于 2023年6月19日 10:22:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定