问题描述
我是python编程的新手,并且正在做一些实验.希望我的问题不是很愚蠢:我正在编写一个小程序,将输入添加到列表中,并在输入等于4时使用while循环将其打印出来.问题是它永远不会停止添加输入并打印列表.我的代码是:
I'm new to python programming and I'm doing some experiments with it. Hoping my question is not too silly: I'm writing a little program that adds inputs to a list and print it when the input equals to 4, using a while loop. the problem is it never stops to add inputs and print the list.My code is:
S=input()
L=[]
while S!=4:
L.append(S)
if S==4:
break
print(L)
推荐答案
您已在代码中创建了一个无限循环.进入循环后,您无需更改S的值.您应该像这样从循环内部添加获取另一个输入:
You've created an infinite loop in your code. You're not changing the value of S after you enter the loop. You should add get an another input from inside the loop like so:
S=input()
L=[]
while S!=4:
L.append(S)
S = input()
print(L)
此外,代码中的if条件是无用的,因为它已经在while循环声明中设置了
In addition, The if condition in your code is useless since It's already set in the while loop declaration
这篇关于将用户输入追加到循环列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!