尝试创建一个简单的程序,其中包含“supercalifragilisticexpialidocious”这个单词并将其显示为辅音替换为短划线。这是代码:
message = print("supercalifragilisticexpialidocious")
result_str = ""
CONSONANTS = "BCDFGHJKLMNPQRSTVXZWY"
print()
for letter in message:
if letter not in CONSONANTS:
result_str += letter
else:
result_str += "-"
print(result_str)
input("\n\nPress the enter key to exit:")
当我在模块中运行它时,它会正确打印"message"
,但会出现此错误而不是打印"result_str"
:
Traceback (most recent call last):
File "F:/CSC119/final exam program 4.py", line 13, in <module>
for letter in message:
TypeError: 'NoneType' object is not iterable
那么我的问题是什么? 谢谢。
答案 0 :(得分:2)
message = "supercalifragilisticexpialidocious"
不打印?
答案 1 :(得分:2)
您无法为变量分配print语句。您需要message = text
而不是message = print(text)