我正在參加佐治亞理工學院的一門課程,我整個晚上都在試圖解決這個問題,但我沒能做到。我的任務如下:
撰寫一個名為 的函式
my_TAs。該函式應該采取作為輸入三個字串:first_TA,second_TA,和third_TA。它應該作為輸出回傳字串,"[first_TA], [second_TA],#and [third_TA] are awesome!",其中的值替換了變數名稱。例如,
my_TAs("Sridevi", "Lucy", "Xu")將回傳字串"Sridevi, Lucy, and Xu are awesome!"。提示:請注意,因為您回傳的是字串而不是列印字串,所以不能使用該
print()陳述句——您必須自己創建字串,然后回傳它。
我的函式回傳"Joshua are awesome"而不是所有三個變數名稱。我試過這個
result = str(first_TA), str(second_TA), str(third_TA) "are awesome!"
但沒有用。
def my_TAs(first_TA, second_TA, third_TA):
result = str(first_TA) " are Awesome!"
return result
first_TA = "Joshua"
second_TA = "Jackie"
third_TA = "Marguerite"
test_first_TA = "Joshua"
test_second_TA = "Jackie"
test_third_TA = "Marguerite"
print(my_TAs(test_first_TA, test_second_TA, test_third_TA))
uj5u.com熱心網友回復:
您可以使用f-Strings來完成此操作:
def my_TAs(first_TA, second_TA, third_TA):
return f"{first_TA}, {second_TA}, and {third_TA} are awesome!"
test_first_TA = "Joshua"
test_second_TA = "Jackie"
test_third_TA = "Marguerite"
print(my_TAs(test_first_TA, test_second_TA, test_third_TA))
輸出:
Joshua, Jackie, and Marguerite are awesome!
uj5u.com熱心網友回復:
使用 代替,
def my_TAs(first_TA, second_TA, third_TA):
result = str(first_TA) ", " str(second_TA) ", and " str(third_TA)
" are Awesome!"
return result
first_TA = "Joshua"
second_TA = "Jackie"
third_TA = "Marguerite"
test_first_TA = "Joshua"
test_second_TA = "Jackie"
test_third_TA = "Marguerite"
print(my_TAs(test_first_TA, test_second_TA, test_third_TA))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/358064.html
上一篇:如何在React中將字串和變數連接到另一個變數的名稱中?
下一篇:匯入類函式變數
