blockRegexes 中包含空白
我正在尝试做一个数学测验:问题的正确答案由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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然这可能不是最优雅的解决方案,但您可以使用 applyFunc 参数传递一个函数,该函数会将空白字符串输入转换为评估为不正确的内容。
, '!', s)这将使用内置的
re
模块将空白字符串或仅包含空格的字符串转换为'!'
。当然,这可以调整为任何保证评估为不正确的内容。然后可以将其作为附加关键字参数应用于 pyinputplus.inputStr 函数调用:
While this may not be the most elegant solution, you can use the
, '!', s)applyFunc
parameter to pass a function that will convert blank string inputs to something that will evaluate as incorrect.This will convert blank strings or strings consisting only of space to
'!'
using the builtinre
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: