You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3打印屏幕输出偶现多余空格问题排查求助

解决Python 3游戏printscreen函数偶现多余空行问题

问题现象

在Python 3开发的游戏项目中,主游戏循环每次启动都会调用printscreen()函数。该函数的输出偶尔会在资源打印内容与物品描述之间出现多余空行,导致顶部文本被挤出显示框,且问题并非持续触发,难以定位根因。

正常输出示例:

[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

排查过程

为定位问题,已执行以下操作:

  • 反复检查问题相关代码区域,未发现明显逻辑错误
  • 打印screenrendersize生成的空行,未发现异常
  • 搜索同类问题,无匹配结果
  • 尝试更换相关变量,问题依旧
  • 一度怀疑是Python 3的固有缺陷

解决方案

通过为更多print语句添加sep=''参数,并在对应字符串末尾添加空格(或双空格)的方式,彻底解决了该问题。

修改后的完整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, "]", sep='')
    elif PCoal >= 1:
        print("[ Stone:", PStone, "] [ Wood:", PWood, "] [ Feather:", PFeather, "] [ Coal:", PCoal, "]", sep='')
    elif PFeather >= 1:
        print("[ Stone:", PStone, "] [ Wood:", PWood, "] [ Feather:", PFeather, "]", sep='')
    elif PWood >= 1:
        print("[ Stone:", PStone, "] [ Wood:", PWood, "]", sep='')
    else:
        print("[ Stone:", PStone, "]", sep='')
    
    if PCell >= 1:
        print("{ Cell:", PCell, "}", sep='')
        screenrendersize -= 1
    
    if UCopperRod >= 1:
        print("0==) x", URollingLog, "   ", ISnareDisplay, "x", UBirdSnare, "    (_) x", UBeggarHat, "    _i_ x", UCopperRod, sep='')
        screenrendersize -= 1
    elif UBeggarHat >= 1:
        print("0==) x", URollingLog, "   ", ISnareDisplay, "x", UBirdSnare, "    (_) x", UBeggarHat, sep='')
        screenrendersize -= 1
    elif UBirdSnare >= 1:
        print("0==) x", URollingLog, "   ", ISnareDisplay, "x", UBirdSnare, sep='')
        screenrendersize -= 1
    elif URollingLog >= 1:
        print("0==) x", URollingLog, sep='')
        screenrendersize -= 1
    
    if UForge == True:
        print("'", IForgeDisplay, "' ", UForgeStatus, sep='')
        screenrendersize -= 1
    
    for i in range(0, screenrendersize):
        print("")

原理说明

Python的print()函数默认会用空格分隔多个参数,若某个参数(如资源数量变量)偶尔包含不可见的换行符或空白字符,就会触发多余空行。添加sep=''后,强制取消参数间的默认分隔,同时手动在字符串中控制空格,确保输出格式稳定。


内容的提问来源于stack exchange,提问作者Oraes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 14:45:01