我目前正在使用 Python 制作游戏。
我希望代码为:
[00:00:00] Name|Hello!
这是我的代码:
print(Fore.YELLOW + Style.BRIGHT + '['),
print strftime("%H:%M:%S"),
print ']',
print(Style.BRIGHT + Fore.RED + ' Name'),
print(Fore.BLACK + '|'),
print(Fore.WHITE + Style.DIM + 'Hello!')
time.sleep(5)
相反 - 由于某种原因 - 它变成了这样:
[ 00:00:00 ] Name | Hello!
我不知道这段代码有什么问题,也不知道如何修复它。
如果能得到所有帮助,我将不胜感激!谢谢。
请您参考如下方法:
使用单个 print 语句和一个逗号打印总是打印尾随空格。
要么使用 one 打印语句将所有内容连接起来,要么使用 sys.stdout.write() 直接写入终端而无需额外的空格:
print Fore.YELLOW + Style.BRIGHT + '[' + strftime("%H:%M:%S") + ']',
或
sys.stdout.write(Fore.YELLOW + Style.BRIGHT + '[')
sys.stdout.write(strftime("%H:%M:%S"))
sys.stdout.write(']')
或使用字符串格式:
print '{Fore.YELLOW}{Style.BRIGHT}[{time}] {Style.BRIGHT}{Fore.RED} Name {Fore.BLACK}| {Fore.WHITE}{Style.DIM}Hello!'.format(
Style=Style, Fore=Fore, time=strftime("%H:%M:%S"))
