I am writing a python code that should convert str to int and find the sum of the inputs. Here's the code:
def convert_add():
strings=[]
n=int(input('Enter the list size: '))
for i in range(0,n):
print('Enter a string:',i)
item=(input())
if item==str:
strings.append(len(item))
print(sum(strings))
else:
strings.append(int(item))
print(sum(strings))
convert_add()
So here's the thing, when I run this code it sums up the length of the strings I input, but I also want it to sum up integers. Since its a len function inputting a number(int) does the same thing it does to a string, it counts the figures, but I want an output of lets say 10 and 20 to be 30 and not 4. I have tried using the if statement and it's not working, i.e if item==str use the len fn else it sums. I am still a beginner and would love to know if there is a way I can combine them. Thanks.
strings.append(len(item))-->strings.append(int(item))itemwill never be equal tostr, asstris a builtin type. What are you trying to accomplish here? The return type from theinputfunction is always of typestr. Do a bit of research in detecting if a string contains only digits, or alpha characters. (I believe that’s what you’re trying to accomplish here(?))