python - 有人可以解释如何存储和显示用户输入的行数吗?

标签 python python-2.7

这个简单的 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/

相关文章:

python - 将命令提示符输出重定向到 python 生成的窗口

python - 正确模拟在另一个 celery 任务中调用的 celery 任务

python - 为什么我得到 'numpy.ndarray' 对象没有属性 'convert' ?

python - 使用 CSV 中的数据迭代 URL 以进行 API 数据拉取 - Python

python - 从列表中删除重复元素

python - Boto 3 卡在 Lambda 中

python - 如何将前置条件应用于 GroupBy 或如何忽略 GroupBy 中具有一条记录的组

python - 如何在一行中的至少一个元素中选择包含特定值的行?

python - 使用 pip 安装 Nose ,但 bash 无法识别 mac 上的命令

Python数据描述符不能作为实例变量工作?