【问题标题】:Enter input continuously on same line在同一行连续输入输入
【发布时间】:2021-01-07 09:33:54
【问题描述】:

我做了一个循环,让用户输入数字,直到他输入“13”,然后循环就会中断。

while True:
       number = int(input())
       if number == 13:
           print("goodbye")
           break

当我运行这个程序时,它看起来像这样:

1
2
3
13
goodbye

但我希望输入在我按如下方式输入时在同一行继续:

1 2 3 13
goodbye

我该怎么做?

【问题讨论】:

  • 如果你希望能够在每个数字的输入之间按回车,这是不可能的。
  • 我认为不可能这样做,因为每次输入时,它都会在另一行打印,因为输入不处理 end = '' 之类的参数
  • 是的,ANSI 控制字符可以工作,但这取决于 OP 在哪里运行程序。 Windows IDLE 会失败。

标签: python input


【解决方案1】:

一种相对简单的方法是:

import os
prompt = ''
while True:
    number = input(prompt)
    os.system('cls')
    # prompt += number + ' ' #If you want the control input to be printed, keep this, remove the other
    if int(number) == 13:
        print(prompt)
        print('goodbye')
        break
    prompt += number + ' '    # add this line before the if starts, if you want to keep 13
    os.system('cls')

输出:

1 2 3 4
goodbye

注意,这不会像您在 cmets 中想要的那样打印 13。但是,如果您想保留它,可以将其移动到if 条件开始之前的行。

这适用于 Windows 命令提示符,不能保证不同的 IDLE/iPython 等。

注意: 这将清除控制台中的所有行,并重写它们。这适用于您的特定问题,但如果之前还打印过其他内容,则可能并不理想。如果你想避免这种情况,你必须在点击这部分代码之前将sys.stdout重定向到file或某种string buffer,然后恢复stdout并将缓冲区的内容作为文本存储在上面使用的prompt 变量。

【讨论】:

    【解决方案2】:

    您需要一种可以让您一次读取一个字符而无需等待换行符的东西。 This question 提供了多种解决方案。我在这里做了一个小例子,它使用 getch 包装器,它应该是跨平台的(可通过 pip 或类似方式安装):

    from sys import stdout
    from getch import getch
    
    lines = []
    line = []
    
    while True:
        k = getch()
        if k == '\n':
            item = ''.join(line)
            line.clear()
            lines.append(item)
            try:
                if int(item) == 13:
                    stdout.write(k)
                    break
            except ValueError:
                pass
            stdout.write(' ')
        else:
            stdout.write(k)
            line.append(k)
        stdout.flush()
    
    print('goodbye!')
    print(lines)
    

    虽然它没有开箱即用的退格等功能,但它可以满足您对正确输入的要求,并且您可能可以实现它们。这是输入 asdf\n48484\n13\n 的示例运行:

    asdf 48484 13
    goodbye!
    ['asdf', '48484', '13']
    

    【讨论】:

    • 我喜欢这个答案,但我的 python 似乎没有'getch'模块:/
    • @redshorts17。您必须从 pypi 安装它(例如使用 pip 或 conda),或使用链接答案的解决方案之一...
    【解决方案3】:

    这是可能的,但解决方案取决于系统。这应该适用于 Windows 10,但有可能您必须更改它的一些设置(现在我不知道哪个 EDIT: This Setting )。诀窍是使用 ansi 控制字符,用于更改控制台:

    import sys
    
    UP_ONE_LINE = "\033[A" # up 1 line
    DELETE_LINE = "\033[K" # clear line 
    
    number = []
    while True:
            #number = int(input())
            number.append(int(input()))
            sys.stdout.write(UP_ONE_LINE)
            sys.stdout.write(DELETE_LINE)
            print(number,end="")
            if number[-1] == 13:
               print("\ngoodbye")
               break
    

    它删除最后一行,然后写入所有数字。您也可以使用控制字符转到最后一行的末尾,但我使用的是这些字符。在我的控制台中,结果是:

    [1, 2, 3, 4, 13]
    goodbye
    

    【讨论】:

    • 该死的我用的是 Windows 7 :(
    • 我在 Windows 10 上运行了这个,也不起作用:(
    【解决方案4】:

    Python continue from input with the space key

    import sys
    import getch
    input = ""
    while True:
        key = ord(getch.getch())
        sys.stdout.write(chr(key))
        sys.stdout.flush()
        # If the key is space
        if  key != 32:
            input += chr(key)
        else:
            input =   ""
        if input == "13":
            print("\nbye")
            break
    
    

    【讨论】:

      猜你喜欢
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多