2017-08-05 5 views
0

私は最初のリストと一致するID番号をユーザーに入力させ、次の2つのリストからインデックスに一致する名前と料金を引いています。ただし、リスト内の従業員IDが見つかりません。私はさまざまなループを試しましたが、完全にエラーが出たり、 "0"の戻り値のような単一の数値を付けたり、最初のリストと一致しません。3つの別々のリストとパラレルアクセスでデータを保存する

これはクラス割り当てのためのものですが、私がしようとしているのと同様のクラスまたはオンラインの例は見つかりません。私はemployee_id_promptセクションでさまざまなループを試しましたが、動作させることができません。

誰かが入力を最初のリストと一致させ、2番目と3番目のリストから一致する値を返す方法について、いくつかのガイダンスを提供できますか?私は "employee_idで見つかった"、 "input_employee_id in employee_id"などの束を試しましたが、私が把握できないエラーを見つけ出しています。

SIZE = 10 
employee_id = [1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010] 
employee_name = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"] 
employee_pay = [17.75, 18.23, 19.42, 20.00, 21.50, 22.34, 23.74, 24.68, 24.75, 25.00] 

def check_int(prompt): 
    int_result = 0 

    while True: 
     try: 
      int_result = int(input(prompt)) 
     except ValueError: 
      print("This is outside the range or invalid. Please enter again.") 
      continue 
     else: 
      break 
    return int_result 


def employee_id_prompt(): 
    input_employee_id = 0 
    found = False 
    index = 0 

    while True: 
     input_employee_id = check_int("Please enter your Employee ID number: ") 
     while found == False and index <= SIZE - 1: 
      if employee_id[index] == input_employee_id: 
       found = True 
      else: 
       index = index + 1 

     if found == True: 
      print("Welcome " + employee_name[index] + ". Your current pay rate is $", + employee_pay[index], 
       "an hour.") 
      break 
     else: 
      print("I'm sorry, that Employee ID number is not recognized.") 
      index = index + 1 


employee_id_prompt() 
+0

これは実際には3つのリストにデータを構造化する良い方法ではありません。その場合は、何らかの種類のレコード(オブジェクト、タプル、namedtupleなど)を使用する必要があります。 –

+0

あなたの疑問にお答えします(https://stackoverflow.com/help/someone-answers)。それはコミュニティを助ける。 –

答えて

0

修正

インデックスを検索するためのlist.indexを使用してください。アイテムが存在しない場合は、ValueErrorが発生し、捕まえることができます。

ix = None 
try: 
    ix = employee_id.index(input_employee_id) 
except ValueError: 
    pass 

if ix != None: 
    name = employee_name[ix] 
    pay = employee_pay[ix] 

改善

あなたのデータを格納するための辞書を使用してください。 collections.defaultdictimport collections最初)を使用して、例えば

{ id1 : (name1, pay1), ... } 

:あなたのような何かを持っていると思いますので、値として支払う、キー、および名前のタプルとしてemployeeIdに使用

In [537]: data = collections.defaultdict(lambda: None) 

In [538]: for id, name, pay in zip(employee_id, employee_name, employee_pay): 
    ...:  data[id] = (name, pay) 
    ...:  

In [539]: data 
Out[539]: 
defaultdict(<function __main__.<lambda>>, 
      {1001: ('A', 17.75), 
      1002: ('B', 18.23), 
      1003: ('C', 19.42), 
      1004: ('D', 20.0), 
      1005: ('E', 21.5), 
      1006: ('F', 22.34), 
      1007: ('G', 23.74), 
      1008: ('H', 24.68), 
      1009: ('I', 24.75), 
      1010: ('J', 25.0)}) 

、あなたのデータを取得するのは簡単です:idが存在しない場合

In [543]: print(data[1001]) 
('A', 17.75) 

In [544]: print(data[1234567]) 
None 

Noneが返されます。これはcollections.defaultdictの機能です。

また、ここではcollections.namedtuple構造が便利な構造ですが、名前付きタプルのリストが必要なので、idで定数検索を犠牲にします。それはfoundindexのpythonのもう少し経験をwhile Trueループ

while True: 
    found = False 
    index = 0 
    input_employee_id = check_int("Please enter your Employee ID number: ") 
    while index <= SIZE - 1: 
     if employee_id[index] == input_employee_id: 
      found = True 
      break 
     else: 
      index = index + 1 

の内側に初期化されなければならないようだ

0

Coldspeedが言うように、あなたは、リストを検索するlist.index()を好むでしょう。

+0

ありがとう!これはうまくいった。私のクラスの例では、2つはTrueのステートメントの中で初期化されていましたが、説明されていませんでした。 – Kethi

+0

これは、 'input_employee_id = check_int(...)'を実行するたびに、新しい従業員IDが処理されているため、検索を新しく再開する必要があるからです。 – Gribouillis

+0

@Kethiあなたがやっているのと同じことをする良い方法があるとき、なぜそれらを使うことを考えないのですか? –

関連する問題