2011-12-15 52 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

如果您需要双向字典,请使用两个字典。