所以我必須在假設評分者定義以下變數的同時撰寫代碼
a to be some int
b to be some float
s to be some string
然后我必須撰寫一個程式來準確列印出以下內容,其中 < .... > 中的值被變數中的實際值替換。但是,您只能使用 ONE 列印陳述句。
// The variable 'a' has value of <a>. //
\\ While the variable 'b' has value of <b>. \\
// Lastly, the variable 's' has value of "<s>". //
例如,如果 a=1、b=1.5 和 s='hi',則預期輸出為:
// The variable 'a' has value of 1. //
\\ While the variable 'b' has value of 1.5. \\
// Lastly, the variable 's' has value of "hi". //
這是我到目前為止所擁有的,這顯然不起作用......
a = int
b = folat
s = str
print(f"// The variable 'a' has value of <{a}> . //\n \\ While the variable 'b' has value of <{b}> . \\ \\n// Lastly, the variable 's' has value of <{s}>. //")
我應該做哪些改變??
uj5u.com熱心網友回復:
您可以將 int 和 float 轉換為字串并將它們全部連接到print陳述句中。您還需要將\字符加倍,因為它是轉義字符,并且與后續字符一起用作單個字符:
a = 1
b = 1.0
s = "a"
print("// The variable 'a' has value of " str(a) ". //\n\\\\ While the variable 'b' has value of " str(b) ". \\\\ \n// Lastly, the variable 's' has value of " s ". //")
輸出:
// The variable 'a' has value of 1. //
\\ While the variable 'b' has value of 1.0. \\
// Lastly, the variable 's' has value of a. //
uj5u.com熱心網友回復:
考慮在 python 中使用多行列印功能。您還需要轉義 the\因為它本身是一個轉義字符,例如\n是換行符而不是字面列印\n。這樣做\\意味著只是\
a = 1
b = 1.5
s = "hi"
print(
f"""// The variable 'a' has value of {a}. //
\\\\ While the variable 'b' has value of {b}. \\\\
// Lastly, the variable 's' has value of "{s}". //"""
)
我個人認為這是最容易閱讀和清理的,因為它看起來更像是所需的輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/368263.html
下一篇:平均矩陣中的唯一值
