能得到正确意向的代码:
def break_words(stuff):
words=stuff.split(' ')
print(words)
sentence="All good things come to those who wait."
break_words(sentence)
其输出是:
['All','good','things','come','to','those','who','wait']
然而,为什么下列代码没有输出或错误呢?
1️⃣,代码如下:
def break_words(stuff):
words=stuff.split(' ')
return words
print(words)
sentence="All good things come to those who wait."
break_words(sentence)
其输出结果是:
No Output
2️⃣,代码如下:
def break_words(stuff):
words=stuff.split(' ')
return words
sentence="All good things come to those who wait."
break_words(sentence)
print(words)
其输出结果是:
Traceback (most recent call last):
File "file0.py", line7, in <module>
print(words)
NameError: name 'words' is not defined.
能得到您的帮助,是我莫大的荣幸,谢谢您,希望您能给予我帮助,谢谢!