python автоматическое взаимодействие с другой программой - PullRequest
0 голосов
/ 02 апреля 2020

У меня есть такой скрипт:

# coding:utf8
# python2

import random

first_num = str(random.randint(0, 10))
second_num = str(random.randint(0, 10))

first_input = raw_input('input %s: ' % first_num)
second_input = raw_input('input %s: ' % second_num)

if first_num==first_input and second_num==second_input:
    print('Right!')
else:
    print('Wrong!')

Если вам нужно взаимодействовать с ним другим python скриптом, как вы всегда можете получить «Right!»?

Ответы [ 2 ]

0 голосов
/ 03 апреля 2020

На linux, я могу использовать pexpect, и я пишу такой скрипт:

# coding:utf8
# python2

'''
input 9: 9
input 1: 1
Right!
'''

import pexpect

p = pexpect.spawn('python simple_check.py')

p.expect(': ')
first_num = p.before.split(' ')[1]
p.send(first_num+'\n')
p.expect(first_num)

p.expect(': ')
second_num = p.before.split(' ')[1]
p.sendline(second_num)
p.interact()

Вывод:

4
Right!

По крайней мере, он работает на linux. Я постараюсь найти способ на windows.

0 голосов
/ 02 апреля 2020

Вы можете импортировать скрипт / модуль после first_num = str (random.randint (0, 10)) second_num = str (random.randint (0, 10)) как

first_num = str(random.randint(0, 10))
second_num = str(random.randint(0, 10))
import <yourscript/module>
...