Python类作业打印按钮输出带括号逗号,如何修复并排查错误?
问题原因
你输出按钮中间行的f-string写法错误,在{}内用逗号分隔多个变量时,Python会将这部分内容识别为元组类型,打印时就会自带元组的括号、逗号、引号格式。
另外你定义的emp = ''是空字符串,乘以空格数后还是空值,也会导致按钮文字两侧没有预期的空格。
修复方案
删除所有打印中间行的f-string里的逗号,直接拼接各部分内容,同时把空字符串换成空格字符' '生成两侧空白,不需要单独定义emp变量。
需要修改的三行代码对应如下:
# 原b1错误写法 # print(f'{b1.border,(emp*spaces),b1.word,(emp*b1.spaces),b1.border}') # 修正后 print(f"{b1.border}{' '*b1.spaces}{b1.word}{' '*b1.spaces}{b1.border}") # 原b2错误写法 # print(f'{b2.border,(emp*b2.spaces),b2.word,(emp*b2.spaces),b2.border}') # 修正后 print(f"{b2.border}{' '*b2.spaces}{b2.word}{' '*b2.spaces}{b2.border}") # 原b3错误写法 # print(f'{b3.border,(emp*b3.spaces),b3.word,(emp*b3.spaces),b3.border}') # 修正后 print(f"{b3.border}{' '*b3.spaces}{b3.word}{' '*b3.spaces}{b3.border}")
修改后运行即可得到预期的按钮输出格式。
内容的提问来源于stack exchange,提问作者Irrelevant
相关产品推荐
相关产品推荐

