我编写了一个代码,该代码必须打开文件,读取文件,查找问题和答案,然后使用pynput将所有这些信息输入到名为“ iren”的程序中。 因此,问题是,代码发现了问题,但只为第一个问题写了答案,我该如何解决?
例如文本文件: 1.这里有些问题: A)第一个答案 B)第二 C)等...
def new_question(text):
keyboard.press(Key.f2)
keyboard.release(Key.f2)
keyboard.type(text)
def new_answer(text):
keyboard.press(Key.f5)
keyboard.release(Key.f5)
keyboard.type(text)
def find_between(text, first, last):
try:
if (text[text.index(str(first)) + 1: text.index(str(first)) + 2]) == ".":
start = text.index(str(first)) + 2 # +2 is to skip the number and dot
end = text.find(last, start)
question = text[start:end]
new_question(question)
except ValueError:
return ""
def find_between_answrs(text, first, last):
try:
frst = text.index(first)
lst = text.index(last)
if (text[frst + 1: frst + 2]) == ")" and (text[lst + 1: lst + 2]) == ")":
start = text.index(str(first)) + 2 # +2 is to skip the the letter and ) symbol
end = text.find(last, start)
answer = text[start:end]
new_answer(answer)
except ValueError:
return ""
time.sleep(5)
for i in range(11):
find_between(cont, i, ":")
for s in range(5):
if s == 0:
strt = "A"
nd = "B"
elif s == 1:
strt = "B"
nd = "C"
elif s == 2:
strt = "C"
nd = "D"
elif s == 3:
strt = "D"
nd = "E"
elif s == 4:
strt = "E"
nd = str(i)
find_between_answrs(cont, strt, nd)