Reputation: 3057
So the task i am trying to accomplish is for two string inputs such as '1' + '2' to return '3',
I would like to be able to be able to do something like this,
d = {'1': 1, '2': 2, '3': 3}
So i have a dictionary like this ^, then I can do
d.get('1')
In hopes it would return 1, except it returns None, How would I work around this?
So thanks to your help i got it to work sort of, although for some reason it only accepts digits which add to 4 and lower, Here is the code so you may better understand
def code_char(char, key):
d = {'1': 1, '2':2 ,'3': 3 ,'4': 4,'5': 5 ,'6': 6 ,'7': 7 ,'8' :8 ,'9':9}
f = {1: '1', 2: '2' ,3: '3' ,4: '4' ,'5':5 ,'6':6 ,'7':7 ,'8':8 ,'9':9}
sum = d.get(char)+d.get(key)
if sum < 9:
print(f.get(sum))
else:
sum = sum % 10
value = f.get(sum)
print(value)
code_char('1','5')
For some reason code+char('1','3')
will correctly return 3 but any higher and it will just print None.
It's the start to my encrypter, thanks for the help so far!
Upvotes: 0
Views: 103
Reputation: 103844
You can use braces:
>>> d = {'1': 1, '2': 2, '3': 3}
>>> d['1']+d['2']
3
Or, if you want a string result:
>>> str(d['1']+d['2'])
'3'
But really -- no mapping is required to do this:
>>> str(int('22')+int('33'))
'55'
Upvotes: 0
Reputation: 390
This is how it works: D.get(k[,d=None]) -> D[k] if k in D else d
. Be sure to use '1' instead of 1.
Upvotes: 0
Reputation: 3274
No idea why it returns None for you. Is that what you actually tried?
> d = {'1': 1, '2': 2, '3': 3}
> d.get('1')
1
Btw, you can turn string to int by int function.
> int('1') + int('2')
3
Upvotes: 1