+ 1
How print work in this case?
words = ["hello", "world", "spam", "eggs"] #for words in words: print(words + "!") This snippet is not working words = ["hello", "world", "spam", "eggs"] for words in words: print(words + "!") This works how print different in both cases
2 Answers
+ 4
In:
words = ["hello", "world", "spam", "eggs"]
#for words in words:
print(words + "!")
You tried to concatenate a list ["hello", ... "eggs"] and a string "!", which raised an error.
In:
words = ["hello", "world", "spam", "eggs"]
for words in words:
print(words + "!")
An indentation error would have been raised because a codeblock statement lacked required indent.
Fixed. in:
words = ["hello", "world", "spam", "eggs"]
for words in words:
print(words + "!")
Program concatenated:
"hello" + "!"
"world" + "!"
"spam" + "!"
"eggs" + "!"
And printed:
hello!
world!
spam!
eggs!
+ 3
Because the first for is an comment?