首页 > 解决方案 > Is there a way to write an if statement to catch an input and automatically enter a string?

问题描述

import pexpect
from BODMASCalculator import math

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

i have been trying to use pexpect to automatically enter 'n' when this input is returned but im having no luck so far

any help greatly appreciated

标签: pythonpexpect

解决方案


尝试如下(仅在 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}')
            

推荐阅读