一連の従業員の姓名、ID 番号、性別、経験年数をリストしたテキスト ファイルがあります。最初の行には、従業員数が含まれています。これは、employees.txt と呼ばれます。ここにあります:
7
ジョン
ドウ
33
272838
M
メアリー
ジョンソン
38
3849383
ふ
オピー
犬
6
839293
M
ミッシー
犬
4
238392
ふ
ジョン
ブラ
28
834282
M
アメリア
スピエン
2
5789812
ふ
シェーン
カセンシュテン
2
567891
M
この情報を辞書のリストに整理し、すべての女性従業員を見つけて、姓と名を出力したいと考えています。辞書のリストは取得できますが、女性従業員の名前を検索して出力しようとするとエラーが発生します。これが私のコードです:
def employee_write(file):
employee_dict = {}
employee_dict["First"] = file.readline().strip()
employee_dict["Last"] = file.readline().strip()
employee_dict["Experience"] = file.readline().strip()
employee_dict["ID"] = file.readline().strip()
employee_dict["Gender"] = file.readline().strip()
return employee_dict
def female_print(x2):
for term in x2:
for word in employee_dict:
if employee_dict["Gender"] == "F":
print(employee_dict["First"])
print(employee_dict["Last"])
def main():
file = open("employees.txt", "r")
n = int(file.readline())
x1 = employee_write(file)
employee_list = []
for i in range(n):
employee_list.append(x1)
x2 = employee_list
print(x2)
female_print(x2)
main()
ここで私のエラーは何ですか? 「employee_dict」という名前が定義されていないと表示されるため、female_print() 関数が機能していません。どうしたの?