Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用户输入被错误地附加到Python列表_Python_Python 3.x_List_Data Structures_Append - Fatal编程技术网

用户输入被错误地附加到Python列表

用户输入被错误地附加到Python列表,python,python-3.x,list,data-structures,append,Python,Python 3.x,List,Data Structures,Append,这里是Python新手 我的程序提示用户输入他们的名字。只有一个“正确”的名字,那就是约翰。当用户输入一个不正确的名字(即不是John)时,程序会将不正确的名字添加到列表中。一旦输入正确的姓名,John,我想打印错误姓名列表 我遇到/不明白的问题是,当输入John时,它已被添加到错误姓名列表中。下面是我的代码: incorrectNames = [] name = "" while name != "John": name = input("Enter your name: \n")

这里是Python新手

我的程序提示用户输入他们的名字。只有一个“正确”的名字,那就是约翰。当用户输入一个不正确的名字(即不是John)时,程序会将不正确的名字添加到列表中。一旦输入正确的姓名,John,我想打印错误姓名列表

我遇到/不明白的问题是,当输入John时,它已被添加到错误姓名列表中。下面是我的代码:

incorrectNames = []
name = ""

while name != "John":
    name = input("Enter your name: \n")
    incorrectNames.append(name)
    if name == "John":
        print("Incorrect names", incorrectNames)
此代码返回[Andy,Sam,John],而不仅仅是[Andy,Sam](如果在John之前输入的两个名字不正确)。这是因为操作顺序吗?我认为一旦变量名为John,就会绕过append指令

任何形式的澄清都将被感激,或者为我指明正确的方向


提前谢谢

其实很简单。无论名称是否等于“John”,您都会将名称附加到列表中。要解决这个问题,只需在if语句之后添加一个else语句

incorrectNames = []
name = ""

while name != "John":
    name = input("Enter your name: \n")
    if name == "John":
        print("Incorrect names", incorrectNames)
    else:
        incorrectNames.append(name)

注意:您可能需要添加一条语句来告诉程序退出循环,否则应用程序将永远不会退出

Python按照您给出的顺序执行指令。这就像一个食谱,如果你写“把蛋糕放进烤箱”作为第一步,写“预热烤箱”作为第二步,你将把蛋糕放进一个冷烤箱

因此,您可以这样修复它:

if name == "John":
    print("Incorrect names", incorrectNames)
incorrectNames.append(name)
然而,这仍然会将约翰添加到列表中。它会在您打印列表后添加他,因此不会显示在输出中,但您可能根本不希望添加他。因此,更好的答案可能是:

if name == "John":
    print("Incorrect names", incorrectNames)
else:
    incorrectNames.append(name)
或者,也可以更容易地将打印名称视为在循环之后执行的操作,而不是在循环内部有条件地执行:

while name != "John":
    name = input("Enter your name: \n")
    if name != "John":
        incorrectNames.append(name)

print("Incorrect names", incorrectNames)
此时,您可能会认为双重检查有点多余,并执行以下操作:

incorrect_names = []

while True:
    name = input("Enter your name: \n")
    if name == "John":
        break
    incorrectNames.append(name)

print("Incorrect names", incorrectNames)


这种配方式排序是大多数编程语言的工作方式,它被称为“命令式”执行。一些“函数式”语言通过根本不让您编写多条指令来避免这种混淆;你必须明确地说“这个函数由另外两个函数组成”或类似的话。其他“数据流”和“逻辑”语言的工作方式更像电子表格,在电子表格中,您可以告诉每个变量如何依赖于其他变量,然后让它计算出获得所需变量值的顺序。但通常,允许您按顺序编写一组指令的语言会强制执行这些指令,就像Python一样。

我将在下面提供更正的代码,然后给出解释:

代码:

incorrectNames = []
name = ""

while name != "John":
    name = input("Enter your name: \n")
    if name != "John":
        incorrectNames.append(name)
    if name == "John":
        print("Incorrect names", incorrectNames)

说明:
试着在运行代码时想象发生了什么。假设我们在用户每次输入后都跟随变量
name
的值和列表
incorrectNames

输入Andy后,
name
的值由于输入语句而变为“Andy”。现在,请注意,紧跟在输入语句之后的是行
incorrectNames.append(name)
。这会导致列表
不正确的名称
变为['Andy']

在此之后,“Sam”也会发生同样的过程。首先输入
名称
,然后将其附加到列表中,使
不相关名称
变成['Andy','Sam']

然后是最后一个循环,当真正的动作发生时

现在用户输入“John”。但问题在于:紧跟在input语句之后的是行
incorrectNames.append(name)
。这意味着,即使不检查
名称
的值,它也会被附加到
不正确的名称


您所要做的就是插入一些代码来检查
name
的值,如果是John,则不要将其附加到
incorrecNames

输入John时,您处于
循环(下一次迭代)中。接下来,将John附加到列表中,然后测试
name
John。如果在追加之前测试John,您将得到想要的结果:

incorrectNames = []
name = ""

while name != "John":
    name = input("Enter your name: \n")
    if name == "John":
        print("Incorrect names", incorrectNames)
    incorrectNames.append(name)

在添加到列表之前,需要检查名称
John
。如果名称等于
John
,则显示所有名称,否则追加到列表中;它的名称始终为
name
,其值为
“John”