-5
    # counter
my_list = input("Input your list.(Numbers)\n")
while my_list.isnumeric() == False:
  print("\n----------------------------------------------------")
  print("***Sorry, what you had entered contained a letter.***")
  print("----------------------------------------------------\n")

  my_list = input("Input your list.(Numbers)\n")
else:
  counter = 0 
  for item in my_list:
    counter_item = counter + item
  print(int(counter_item))

The exact error message is

"Traceback (most recent call last):
  File "main.py", line 13, in <module>
    counter_item = counter + item enter code here 
TypeError: unsupported operand type(s) for +: 'int' and 'str'"

What should I do?

P.S (This is in Python3 on Repl.it)

iGaur
  • 3
  • 3
  • 1
    [Get a list of numbers as input from the user](https://stackoverflow.com/q/4663306/15497888), [TypeError: unsupported operand type(s) for -: 'str' and 'int'](https://stackoverflow.com/q/2376464/15497888), [How to convert user input into a list](https://stackoverflow.com/q/37195604/15497888) – Henry Ecker Jul 24 '21 at 17:06
  • Use the `int()` function to convert a string to int. – Barmar Jul 24 '21 at 17:06
  • 1
    "How would I convert a str to an int?" The same way you did in your final call to `print`. – chepner Jul 24 '21 at 17:06
  • 1
    `isnumeric()` won't be true for a list of a integers, it will only be true if they type a single integer. – Barmar Jul 24 '21 at 17:07
  • The int() is not working it just says the it is an unsupported operand. – iGaur Jul 24 '21 at 17:10
  • `counter += int(item)` should not report that error. – Barmar Jul 24 '21 at 17:11
  • What's the point of using `counter + item`? `counter` is always `0`, and adding `0` doesn't change anything. Did you mean to assign to `counter` instead of `counter_item`? – Barmar Jul 24 '21 at 17:12
  • `my_list` isn't a list, it's a string. `for item in my_list:` is iterating over each character in the string. – Barmar Jul 24 '21 at 17:14
  • Thank You @Barmar, but can you apply what you mean to my code, and post it that way, its very hard for me to follow otherwise. – iGaur Jul 24 '21 at 17:19

6 Answers6

1

First, use split() to split the input into a list.

Then, you need to test isnumeric() on every element of the list, not the whole string.

Call int() to convert the list elements to integers.

Finally, add the sum to counter, not counter_item.

my_list = input("Input your list.(Numbers)\n").split()
while not all(item.isnumeric() for item in my_list):
  print("\n----------------------------------------------------")
  print("***Sorry, what you had entered contained a letter.***")
  print("----------------------------------------------------\n")

  my_list = input("Input your list.(Numbers)\n").split()

counter = 0 
for item in my_list:
    counter = counter + int(item)
print(counter)

You don't need to use else: after the loop. That's only needed if the loop can end using break, and you want to run code only if it ends normally.

You don't need to use int() when printing, since counter is already an integer.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Use .isdigit() instead of .isnumeric(), for checking if input is solely numbers or not.

my_list = input("Input your list.(Numbers)\n")
while not my_list.isdigit():
  print("\n----------------------------------------------------")
  print("***Sorry, what you had entered contained a letter.***")
  print("----------------------------------------------------\n")

  my_list = input("Input your list.(Numbers)\n")
else:
  counter = 0 
  for item in my_list:
    counter +=int(item)
  print(counter)

Also, use +=. It will add the value instead of creating a local variable every time.

The error TypeError: unsupported operand type(s) for +: 'int' and 'str'" states that it cannot add string and an integer.

  • + between 2 strings is for string concatenation.
  • + between 2 integers is for addition.

What you probably want to do is the second one. So convert s to an integer

0

The error indicates that you are adding str and int. You need to convert item to int before adding in counter.

counter_item = counter + int(item)
kamran890
  • 762
  • 4
  • 7
0

You need to convert strings to ints (using the int function) before you can add them. In addition, if your intent is to have the user enter multiple numbers, you probably want to split() the string before converting the numbers into ints -- which means you can't use isnumeric() on the original string to check for input errors, because it will contain spaces!

Here's how I'd do it:

while True:
    try:
        print(sum(map(
            int,
            input("Input your list.(Numbers)\n").split()
        )))
        break
    except ValueError:
        print("\n----------------------------------------------------")
        print("***Sorry, what you had entered contained a letter.***")
        print("----------------------------------------------------\n")

This is a good example of the power of exceptions as an error handling mechanism -- instead of having to individually check each element of the list (as you would have to do in a language that doesn't support exceptions), you can just operate on the list under the assumption that everything will work, and use a single try/except to catch resulting ValueErrors no matter where they get raised in that process. Putting the whole thing in a while loop lets you simply retry until the entire sequence of operations is able to finish with no exceptions.

Example output:

Input your list.(Numbers)
lskdjf

----------------------------------------------------
***Sorry, what you had entered contained a letter.***
----------------------------------------------------

Input your list.(Numbers)
1 2 3 4
10
Samwise
  • 68,105
  • 3
  • 30
  • 44
0
while True:
    input_lst = map(int,input("Enter list of number").split())
    try:
        counter = 0
        for val in input_lst:
            counter += int(val)
        break
    except Exception:
        print ("All the inputs were not numeric.")
print (counter)
Prasanna P
  • 64
  • 1
  • 6
0

your are taking the input in wrong way, input writes a sring into My_list.

my_list = input("Input your list.(Numbers)\n").split(" ")

isnumaric function is applyed to integer data type but not for arry. so you need to apply a for loop or while loop to check each element on arry.

counter is integer data type and item is string data type since input function retuns string, so u need to convert string to int using int()function.. counter_item = counter_item + int(item)

i have made a code which may works for you.

my_list = input("Input your list.(Numbers)\n").split(" ")
print(my_list)
i =0
while i!= len(my_list):
  for item in my_list:
    if item.isnumeric():
      i+=1
      continue
    else:
      i=0
      print("\n----------------------------------------------------")
      print("***Sorry, what you had entered contained a letter.***")
      print("----------------------------------------------------\n")
      my_list = input("Input your list.(Numbers)\n").split(" ")
      break 
else:
  counter_item = 0 
  for item in my_list:
    counter_item = counter_item + int(item)
  print(int(counter_item))