如何使用 PyInputPlus 限制輸入長度及其允許的字符?我做了一些研究并發現了這個問題,但是這個問題沒有專門針對 PyInputPlus 的答案。我想同時允許 'az' 和 'A-Z'。
到目前為止的代碼:
import pyinputplus as pyip
import re
oneLetter = pyip.inputStr(prompt=':')
if not re.match("^[a-z]*$", oneLetter):
print ("Error! Only letters a-z allowed!")
if len(oneLetter)>1:
print('You can only type in 1 character')
else:
print(oneLetter)
期望的輸出:
:1
>Only letters a-z, A-Z allowed
:Bob
>Only 1 letter allowed
:B
>B
uj5u.com熱心網友回復:
您可以嘗試下面的代碼來解決您的問題。我建議更熟悉正則運算式。
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/334531.html
上一篇:沒有這樣的檔案或目錄:'Tensorflow/workspace/annotations\\label_map.pbtxtonJupyter為什么我的代碼不起作用?
