[英]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.