繁体   English   中英

如何在保持格式的同时从循环中打印多行 output?

[英]How to print on the same line an multiline output from a loop while keeping the format?

我正在尝试解决 FCC 的算术格式化程序。 此代码块打印算术运算。 该块位于一个循环中,该循环查看参数是否为操作。 其中一项任务是在同一行中打印 output 并用 4 个空格分隔表达式。 如何在保持格式的同时在同一行打印 output? 我试过 print(,end='') 但我没有保留格式

     if(print_result is None):
        print(
            first_number.rjust(dash_to_print + 1, ' '), '\n',
            operator , second_number.rjust(dash_to_print - 2, ' '), '\n',
            '_'*dash_to_print)

我建议您在 python 中使用更多类似格式化字符串(f-string)的东西

print(f"variableNameIs: {variable:amountDigitsAsNumber}") 
rankings = {"Gonzaga": 31, "Baylor": 28, "Michigan": 25, "Illinois": 24, "Houston": 21}

for team, score in rankings.items():
    print(f"{team:10} ==> {score:10d}")print(f"{team:10} ==> {score:10d}")

output 喜欢:

Gonzaga ==>   31
Baylor  ==>   25
......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM