[Solved] How would I convert a str to an int?


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.

0

solved How would I convert a str to an int?