簡體   English   中英

GUI中有辦法嗎?

[英]Is there a way in the GUI?

您好,我正在嘗試創建一個程序來確認激素水平的懷孕(GCH,它是字典的 int 參數),但在 GUI 中我收到此錯誤:“ifexamen[”GCH”] >= 9 和examen[ "genero"] == F:" TypeError: 字符串索引必須是整數

主要的:

def confirm_pregnancy(exam: dict) -> bool:
 if exam ["GCH"]>= 9 and exam ["gender"] == F:
     pregnancy = True
 else:
     pregnancy = False
 return pregnancy

圖形用戶界面:

def execute_confirm_pregnancy (e1: dict, e2: dict, e3: dict, e4: dict) -> None:

 "" "
 It executes the function that validates if there is pregnancy depending on the levels of HCG in the blood test with
 id given by parameter
  Parameters: e
     e1 (dict): Dictionary with information from exam 1.
     e2 (dict): Dictionary with information from exam 2.
     e3 (dict): Dictionary with the information of the exam 3.
     e4 (dict): Dictionary with the information of the exam 4.
 The program should show the user: "Test results suggest that the patient is pregnant." if he
 Exam confirms it. Otherwise, the message is expected to be: "Test results suggest that the patient is NOT pregnant."
 "" "

 id = input ("Enter the identifier of the patient you want to search for")
 print (mod.confirm_pregnancy (id))
 

您在這里所做的是將錯誤類型的參數傳遞給confirm_pregnancy(exam: dict) function,您在其中傳遞的是字符串而不是字典。

at GUI part of your code in the function execute_confirm_pregnancy you use The input function which is a built-in function reads a line from standard input, converts it to a string (stripping a trailing newline), and returns that. 文檔

如果我們執行您的這行代碼: id = input("Enter the identifier of the patient you want to search for") ,程序會提示我們:

輸入您要搜索的患者的標識符

程序用戶將輸入的任何內容都將作為字符串返回並存儲在id變量中,如果我們假設用戶將輸入例如: GCH 9 gender F ,那么id = 'GCH 9 gender F'這是一個字符串(字符數組)

當您使用id作為此 function 的string type參數調用confirm_pregnancy(id)時,您嘗試將字符串(字符數組)視為鍵值對的字典,因此當 function 嘗試定位id['GCH']它會引發TypeError ,因為id是用 integer 事件索引list of chars ,即id[0] = G, id[1] = C, etc.., but不是像 dict 這樣的key

您應該解析您的用戶輸入並將其存儲為 dict 類型而不是字符串。

您的問題不太清楚字典的外觀以及它包含多少個鍵值對,因此對於如何解析用戶的輸入並將其存儲在 dict.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM