while Loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

while Loop

>>> numbers=list(range(2,20,2)) >>> print(numbers) [2, 4, 6, 8, 10, 12, 14, 16, 18] >>> max_index=len(numbers) >>> print(max_index) 9 >>> x=0 >>> while x<=max_index: y=numbers[x] print(y+"!") x=x+1 Traceback (most recent call last): File "<pyshell#120>", line 3, in <module> print(y+"!") TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> Why cant I get above coding to work. it returns with that error. I want to print each list item with !...

23rd Aug 2017, 12:30 AM
Rohit Sajwan
3 Answers
+ 2
what I mean is the variable y is a integer and you are adding a string to it which caused the error so you need to use str (y) which converts the value of y to a string so you can concatenate it with another string
23rd Aug 2017, 1:55 PM
Enzo
Enzo - avatar
+ 3
it looks like you need to convert y to a string before adding the exclamation mark.
23rd Aug 2017, 1:19 AM
Enzo
Enzo - avatar
0
Thanks pal. now when i converted y to a string I get this..still not getting numbers from the list. >>> x=0 >>> while x<=max_index: y=numbers[x] print("y"+"!") x=x+1 y! y! y! y! y! y! y! y! y!
23rd Aug 2017, 10:21 AM
Rohit Sajwan