【发布时间】:2019-09-02 00:39:41
【问题描述】:
正在研究 Sweigart 的“使用 Python 自动化无聊的东西”。他提供了以下简单列表程序的示例。我完全按照他的编码,但程序会产生一个无限循环。我错过了什么?
在使用此示例时尝试搜索具有相同结果的其他人。找不到任何东西。
catNames=[]
while True:
print('Enter the name of cat ' + str(len(catNames) +1) + ' (Or enter nothing to stop.):')
name=input()
if name == '':
break
catNames = catNames + [name]
print('The cat names are:')
for name in CatNames:
print(' ' + name)
结果应该是:
The cat names are:
tommy
spot
paws
etc.
相反,我得到了第一个打印命令的无限循环。
【问题讨论】:
-
CatNames未定义,但即使修复后我也无法重现该问题。请仔细检查您的代码。参考minimal reproducible example。
标签: python-3.x list