DEV Community

Phinance
Phinance

Posted on

String to Integer program

Hi there,

I’m trying to write a python code that takes in up to 5 inputs (words) and outputs out the sum of the inputs (after a conversion from string to integer). Specifically:

user inputs 5 words (Apple, Pear, Tomato, Banana, Toast)

Program substitutes each words using a defined conversion
Apple = 25
Pear = 72
Tomato = 87
Banana = 98
Toast = 2

Program outputs the sum of the 5 words in integer format (i.e. 25+72+87+98+2)

Would appreciate any help, thank you in advance

Top comments (4)

Collapse
 
amanlalwani007 profile image
amanlalwani007

N=int(input("no of words"))
A=[]
for _ in n:
word=str(input())
A.append(int(word.split('=')[1]))
print(sum(A))

Collapse
 
phinance profile image
Phinance

Wow, so simple and beautiful, thanks

Collapse
 
rodrigomaria profile image
Rodrigo Maria

Please explain your program better. Will the user type these 5 words? Or any of the 5? Can you type in a word other than those?

Collapse
 
phinance profile image
Phinance

Thx to all. The user will type any of the 5 words any number of times, if the user types in a word not defined by a value it should report back a 0 for that word. I hope I explained this well.