-1

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.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Black90
  • 21
  • 1
  • Where did you declare `raw_input1` and `raw_input2`? – Code-Apprentice Jan 14 '18 at 18:47
  • Sorry, was saving space so I didn't write that bit out. Figured people might have guessed by Raw_1 and Raw_2. I'll be clearer next time. – Black90 Jan 14 '18 at 19:08
  • I appreciate that you want to keep your code example as minimal as you can. At the same time, it must be complete. Don't leave anything for others to guess. – Code-Apprentice Jan 14 '18 at 19:11
  • 1
    Please give your expression `if a and b in x` a second thought. It does not, what you think it does. Also, please refrain from naming a dictionary `dict`. You shadow a [built-in function](https://docs.python.org/3/library/functions.html) – Mr. T Jan 14 '18 at 19:19
  • @Piinthesky. Right, thanks. – Black90 Jan 14 '18 at 19:43

2 Answers2

0

You are making this much more difficult than necessary. I suggest writing the steps out in words, then translating the words into Python. For example:

  1. Get the first key from the user
  2. Get the second key from the user
  3. Get the first value from the dictionary
  4. Get the second value from the dictionary
  5. Add the two values together
  6. Print the result
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0
dict = {"one" : 1, "two" : 2, "three" : 3}
total = dict[raw_input1] + dict[raw_input2]
Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75