I want to take a string and then do computation based on the characters in that string.
a =1
b =2
c =3
test = 'cab'
test_sum= [?]
How can I make it so that I make "test_sum" equal to 6?
I want to take a string and then do computation based on the characters in that string.
a =1
b =2
c =3
test = 'cab'
test_sum= [?]
How can I make it so that I make "test_sum" equal to 6?
You can hold a dictionary of character values.
values = {
'a': 1,
'b': 2,
'c': 3,
}
test = 'cab'
test_sum = sum([values[c] for c in test]) # 6