(Mar-11-2017, 08:29 AM)wavic Wrote: It's not wrong.print('Item no. ', (i+1), ' is: ', listA[i])
will work too. It's about routines.
Hello, @SandraDan. As @Scaperen showed you, to concatenate strings they must be strings. listA is an integer.
it did not work, unchanged, for me. i think it should have, but i definitely got an error.
Output:lt1/forums /home/forums 6> cat foo.py
# loop in list
listA = list(range(0,101,5))
for i in range(len(listA)):
print('Item no. ' + str(i+1) + ' is: ' + listA[i])
lt1/forums /home/forums 7> py3 foo.py
Traceback (most recent call last):
File "foo.py", line 6, in <module>
print('Item no. ' + str(i+1) + ' is: ' + listA[i])
TypeError: Can't convert 'int' object to str implicitly
lt1/forums /home/forums 8>
wrapping it in str() made it work. it had been my understanding that print() would apply repr() to any non-string argument it got. i learned i was wrong.(Mar-11-2017, 08:50 AM)buran Wrote:(Mar-11-2017, 08:04 AM)Skaperen Wrote: what is wrong with minimal change?well, why not show a better way, 'pythonic' why?
if that is the direction we want newbies to go in very early then we should explain it in all cases where a non-pythonic way fails and working pythonic code is given. and explaining why [i]that code is pythonic would be a plus.
my look on it is what did the poster do wrong. if what the poster did wrong was not code in a pythonic way, then that should be said.
i highlighted "that" and clicked on the italicize button. it showed up for me correctly in the edit window and in the preview. then when i posted it seems to have lost the closing tag ... maybe a bug in post merging?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.