Search code examples
pythondictionaryadditionraw-input

Adding values from a dictionary


I would like to know how can I, using a raw_input command, pick out the values of two user chosen keys to add together?

What I have attempted is:

dict = {"one" : 1, "two" : 2, "three" : 3}

Total = Sum(v for v in dict.values() if raw_input1 and raw_input2 in dict) 

I did borrow that line from another thread but can't figure out how to shape it the way I need to. All the above achieves is the sum of everything in the dictionary instead of the 2 the user picks.


Solution

  • dict = {"one" : 1, "two" : 2, "three" : 3}
    total = dict[raw_input1] + dict[raw_input2]