这个简单的 while 循环在一个哨兵值处停止,否则不断要求用户输入。
如何使用递增
行计数变量在用户输入某些内容后显示?我想需要使用字典吗?
lineCount = 1
d = {} #is this needed?
q = raw_input("enter something")
while q != "no":
lineCount += 1
q = raw_input("enter something")
#code here that stores each new input with its respective line and prints what the user inputted on each corresponding line when the loop ends
提前非常感谢
最佳答案
使用数组:
lines = []
def add_line(line):
lines.append(line)
def print_lines():
for i in range(len(lines)):
print "%d: %s" % (i, lines[i])
lineCount = 1
q = raw_input("enter something")
add_line(q)
while q != "no":
lineCount += 1
q = raw_input("enter something")
if q != "no":
add_line(q)
print_lines()
关于python - 有人可以解释如何存储和显示用户输入的行数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40912137/