Home > Software design >  I am getting an error when i try to do a Looping String in Python
I am getting an error when i try to do a Looping String in Python

Time:01-28

New to Programming. Currently learning Python.

Code I wrote is:

       x = 0
       for x in "Helper":
       x = x   1
       print(x)

But i get an error message saying "TypeError: cannot concatenate 'str' and 'int' objects on line 82"

Can anyone explain what i did wrong?

CodePudding user response:

x is initialized as int, but you are trying to add two different type (int, string) you should match each type

so try this

y = ""
for x in "Helper":
    y = y   x
print(y)

CodePudding user response:

As the comments say, don't use "x" for BOTH variables.

Do something like this:

x = 0
for c in "Helper":
   x = x   1
print(x)

Result:

6

which is the number of characters in the string "Helper".

  •  Tags:  
  • Related