我是 python 的新手,并嘗試按如下方式執行 f 字串:
next_patient = East_Room.get_highest_priority()
print(f"The next patient is {next_patient.display_symptoms()} please")
其中 East_Room 是 Class 的實體, get_highest_priority 是類中的一種方法,用于顯示具有“嚴重性”屬性最高整數的患者,如下所示:
def get_highest_priority(self):
tmp_priority_patient = None
current_size = self.SLL_waiting_list.size()
counter = 1
while counter <= current_size:
tmp_node = self.SLL_waiting_list.get_node(counter)
tmp_patient = tmp_node.get_obj()
if tmp_priority_patient == None:
tmp_priority_patient = tmp_patient
else:
if tmp_patient.severity > tmp_priority_patient.severity:
tmp_priority_patient = tmp_patient
counter = counter 1
return tmp_priority_patient
def display_symptoms(self):
print(f"{self.firstname} {self.lastname}:{self.symptoms}")
這是輸出:
康納:拿索
下一個病人是沒有請
我知道如果我在沒有 f-string 的情況下呼叫它,這個方法可以完美地作業。謝謝你的幫助!
uj5u.com熱心網友回復:
display_symptoms 只列印資訊但不回傳任何內容。
在 Python 中,不回傳任何內容的函式 return None,因此您得到的輸出是:“請下一個患者是無”
如果您還希望函式回傳此字串,則必須顯式回傳它:
def display_symptoms(self):
print(f"{self.firstname} {self.lastname}: {self.symptoms}")
return f"{self.firstname} {self.lastname}: {self.symptoms}"
一個更好的方法是讓它成為一個屬性:
@property
def display_symptoms(self):
return f"{self.firstname} {self.lastname}: {self.symptoms}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/381768.html
上一篇:Magento2:語法錯誤,意外的'"',在phtml檔案中需要'-'或識別符號(T_STRING)或變數(T_VARIABLE)或數字(T_NUM_
