如何使用字典计算python中两个变量的输入
首先,我想做的是将两个变量转换成一个值和键,然后使用它们对一个常量字典进行求值 期望输出: 输入您的任务:框架 输入(左)线性英尺:1200 框架工作估计为7800美元如何使用字典计算python中两个变量的输入,python,Python,首先,我想做的是将两个变量转换成一个值和键,然后使用它们对一个常量字典进行求值 期望输出: 输入您的任务:框架 输入(左)线性英尺:1200 框架工作估计为7800美元 class Lookup(dict): """ a dictionary class which can lookup value by key, or keys by value """ def __init__(self, items=[]): """items can be
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[]中的键,并将其乘以用户给定的规范输入。假设该值存在于作业目录中。如果您需要一个双向字典,请使用两个字典。您在哪一行得到该错误?你能发布完整的回溯吗?很难说你的代码应该做什么。你能解释一下你需要做些什么,这样我们才能帮助你选择更好的方法吗?此时,输入和预期输出的示例将非常有用。我将更新注释,以添加有关程序应该执行的操作的更多详细信息。
str(输入(“输入您的任务”)
缺少括号