[英]Using PyInputPlus to restrict a string to specific length and allow only certain characters
how do I use PyInputPlus to restrict input length and it's allowed characters.我如何使用 PyInputPlus 来限制输入长度及其允许的字符。 I did some research and found this question , but this question doesn't have answer specifically for PyInputPlus.
我做了一些研究并发现了这个问题,但是这个问题没有专门针对 PyInputPlus 的答案。 And I want to allow both 'az' and 'A-Z'.
我想同时允许 'az' 和 'A-Z'。
Code so far:到目前为止的代码:
import pyinputplus as pyip
oneLetter = pyip.inputStr(prompt=':')
if not re.match("^[a-z]*$", letter):
print ("Error! Only letters a-z allowed!")
if len(letter)>1:
print('You can only type in 1 character')
else:
print(oneLetter)
Desire output:期望输出:
:1
>Only letters a-z, A-Z allowed
:Bob
>Only 1 letter allowed
:B
>B
You may try the code below to fix your issue.您可以尝试下面的代码来解决您的问题。 I would suggest getting more familiar with regular expressions.
我建议更熟悉正则表达式。
import pyinputplus as pyip
import regex as re
oneLetter = pyip.inputStr(prompt=':')
if len(oneLetter) > 1:
print('You can only type in 1 character')
elif not re.match("^[a-zA-Z]$", oneLetter):
print ("Error! Only letters a-z allowed!")
else:
print(oneLetter)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.