2011-12-15 176 views
0

Inititally什麼,我想這樣做是評估兩個變量的輸入兩個變量轉換成價值和鍵,然後用它們評估對一個恆定的字典。如何使用字典中的蟒蛇

所需的輸出:

輸入您的任務:制定

輸入(LF)線性英尺:1200

取景工作估計現在我$ 7,800名

class Lookup(dict): 
    """ 
    a dictionary class which can lookup value by key, or keys by value 
    """ 
    def __init__(self, items=[]): 
     """items can be a list of pair_lists or a dictionary""" 
     dict.__init__(self, items) 

    def get_key(self, value): 
     """find the key(s) as a list given a value""" 
     return [item[0] for item in self.items() if item[1] == value] 

    def get_value(self, key): 
     """find the value given a key""" 
     return self[key] 

    def find_key(dic, val): 
     """return the key of dictionary dic given the value""" 
     return [k for k, v in job_dic.iteritems() if v == val][0] 

    def find_value(dic, key): 
     """return the value of dictionary dic given the key""" 
     return dic[key] 

class JobLookup(dict): 
    """ 
    Where job type and linear feet are input by the user 
    """ 
    def __init__(self, jobvalue="None", specs="0"): 
     self.jobvalue = str(input("Enter your task: ") # e.g. framing 
     self.specs = int(input("Enter (LF): ")) # e.g. 1200 (LF = linear feet) 

    def find_key(dic, val): 
     """return the key of dictionary dic given the value""" 
     return [k for k, v in job_dic.iteritems() if v == val][0] 

    def find_value(dic, key): 
     """return the value of dictionary dic given the key""" 
     return dic[key] 

    def getjobkey(self, jobvalue, specs): 
     """ user inputs job to bid """ 
     while True: 
      if jobvalue == any (job_dict.find_value(job_dict)): 
       return self[key]* specs # 7,800 e.g. 1200 * 6.50 

if __name__ == "__main__": 

# dictionary of interior wall jobs 
job_dic = { 
'layout': .40, 
'framing': 6.50, 
'blocking': 500.00, 
'insulation': .95, 
'firetape': .95, 
'hanging': 8.50, 
'finishing': 5.75, 
'punchout': 6.50 
} 

獲得

>>>NameError : name 'JobLookup' is not defined. 

我甚至不確定此代碼是比較兩個值並隨後評估(在這種情況下是乘法)其相應鍵的正確方法。有沒有更簡單的方法來完成這項任務?

如果輸入的jobvalue ==「value」使用value.job_dict []中的鍵並將其乘以用戶給出的spec輸入。在job_dict中提供該值。

+0

你在哪一行得到那個錯誤?你可以發佈完整的追溯? – 2011-12-15 21:41:04

回答

1

如果您需要雙向字典,請使用兩個字典。