blockRegexes 中包含空白

发布于 2025-01-13 07:32:08 字数 460 浏览 1 评论 0原文

我正在尝试做一个数学测验:问题的正确答案由allowRegexes 覆盖,任何其他不正确的答案由blockRegexes 覆盖 - 除空白之外的任何其他答案。

现在,当我输入空白作为答案时(例如空格),它会显示“不允许使用空白值”并让我重新输入答案。但我希望我的代码在输入为空白时给出“不正确”的答复并跳到下一个问题。 我该怎么做?我尝试添加 if 语句或以某种方式将其添加到“blockRegexes”部分,但失败了。

try:
    pyinputplus.inputStr(task, allowRegexes=['^%s$' % (num1 * num2)], blockRegexes = [('.*', 'Incorrect')],timeout = 5, limit =1)```

Thank you for any suggestions on how can I implement that into my code.

I am trying to make a math quiz: correct answer for the question is covered by allowRegexes and any other incorrect answer is covered by blockRegexes - any other answer but blanks.

Now, when I input a blank as an answer - a space for instance - it shows me "blank values are not allowed" and makes me re-enter my answer. But I want my code to give "incorrect" reply and skip to the next question, when input is a blank.
How do I do it? I tried to add an if statement or somehow add it to "blockRegexes" part, but I failed.

try:
    pyinputplus.inputStr(task, allowRegexes=['^%s
 % (num1 * num2)], blockRegexes = [('.*', 'Incorrect')],timeout = 5, limit =1)```

Thank you for any suggestions on how can I implement that into my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪是无色的血 2025-01-20 07:32:08

虽然这可能不是最优雅的解决方案,但您可以使用 applyFunc 参数传递一个函数,该函数会将空白字符串输入转换为评估为不正确的内容。

import re

def convert_blank(text):
    return re.sub('^\s*

这将使用内置的 re 模块将空白字符串或仅包含空格的字符串转换为 '!'。当然,这可以调整为任何保证评估为不正确的内容。

然后可以将其作为附加关键字参数应用于 pyinputplus.inputStr 函数调用:

pyinputplus.inputStr(
    task, 
    allowRegexes=['^%s
, '!', s)

这将使用内置的 re 模块将空白字符串或仅包含空格的字符串转换为 '!'。当然,这可以调整为任何保证评估为不正确的内容。

然后可以将其作为附加关键字参数应用于 pyinputplus.inputStr 函数调用:


 % (num1 * num2)], 
    blockRegexes = [('.*', 'Incorrect')],
    timeout = 5, 
    limit =1,
    applyFunc=convert_blank,
    )
, '!', s)

这将使用内置的 re 模块将空白字符串或仅包含空格的字符串转换为 '!'。当然,这可以调整为任何保证评估为不正确的内容。

然后可以将其作为附加关键字参数应用于 pyinputplus.inputStr 函数调用:

While this may not be the most elegant solution, you can use the applyFunc parameter to pass a function that will convert blank string inputs to something that will evaluate as incorrect.

import re

def convert_blank(text):
    return re.sub('^\s*

This will convert blank strings or strings consisting only of space to '!' using the builtin re module. This of course can be adjusted to anything that is guaranteed to evaluate as incorrect.

This can then be applied to the pyinputplus.inputStr function call as an additional keyword argument:

pyinputplus.inputStr(
    task, 
    allowRegexes=['^%s
, '!', s)

This will convert blank strings or strings consisting only of space to '!' using the builtin re module. This of course can be adjusted to anything that is guaranteed to evaluate as incorrect.

This can then be applied to the pyinputplus.inputStr function call as an additional keyword argument:


 % (num1 * num2)], 
    blockRegexes = [('.*', 'Incorrect')],
    timeout = 5, 
    limit =1,
    applyFunc=convert_blank,
    )
, '!', s)

This will convert blank strings or strings consisting only of space to '!' using the builtin re module. This of course can be adjusted to anything that is guaranteed to evaluate as incorrect.

This can then be applied to the pyinputplus.inputStr function call as an additional keyword argument:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文