-3
Goal:
Assign values to a string based on a dictionary with keys and values
Example:
I have a string in this format:
str = 'ABCD'
and a dictionary in this.
dic = {'A' : 71, 'B' : 103, 'C' : 115, 'D' : 129}
Script
for i in aa:
soma += dic.get(i, 0)
print(round(soma, 3))
Problem to be solved:
I need to create a for loop to assign values to str and add. Type 71 + 103 + 115 + 129 and give the final sum value (418) and if I assign n values in the dictionary the program is able to give me the final sum.
Thank you to all who can help....
Leo
Hello Lcalado, Welcome to Stack Overflow in English. In your question you are just sharing an exercise statement. What have you tried? The idea of the site is not to do your college work or homework for you, but if you’re having difficulty somewhere specific, share your code, tell us what you’ve tried to do and what the difficulty is, so you increase your chances of getting a good answer.
– Anthony Accioly
Thanks Anthony, I’ll do it next time!!! In this case I kept trying to leave the place and I could not, perhaps for the great inexperience, both here in the stack and with the language.
– LCalado
print(sum(dic[i] for i in str))
– TigerTV.ru