【问题标题】:Is there a way to write an if statement to catch an input and automatically enter a string?有没有办法编写 if 语句来捕获输入并自动输入字符串?
【发布时间】:2020-07-28 10:46:35
【问题描述】:
import pexpect
from BODMASCalculator import math

Math = pexpect.spawn(math('aaaa'))
Math.expect("error incorrect Format try again?(y/n)", 'n')
Math.sendline('n')

我一直在尝试使用 pexpect 在返回此输入时自动输入“n”,但到目前为止我没有运气

非常感谢任何帮助

【问题讨论】:

  • 不清楚你愿意做什么。
  • 基本上我想编写一个函数来在收到提示填写输入时自动输入'n',而无需手动输入'n'

标签: python pexpect


【解决方案1】:

尝试如下(仅在 Windows 平台上):

import msvcrt
while True:
    if msvcrt.kbhit():
        key_stroke = msvcrt.getch()
        if key_stroke == b'n':
            value = input("Please enter a string:\n")
            print(f'You entered {value}')
            

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多