Python 字母后跟数字的字符串输入验证
我希望userinput的格式为字母数字。这些信应该 来自Python 字母后跟数字的字符串输入验证,python,python-3.x,Python,Python 3.x,我希望userinput的格式为字母数字。这些信应该 来自A-H,数字应介于1-7之间,例如AMPELA4或H7。 我不希望用户输入是例如AA,AAA,2B或22。 这就是我到目前为止所做的: x=input("Write something:") if len(x) !=2: print("Wrong") letter=x[0] number= x[1] if number >=8: print("Wrong") if letter not ["A","B","C
A-H
,数字应介于1-7
之间,例如AMPELA4
或H7
。
我不希望用户输入是例如AA
,AAA
,2B
或22
。
这就是我到目前为止所做的:
x=input("Write something:")
if len(x) !=2:
print("Wrong")
letter=x[0]
number= x[1]
if number >=8:
print("Wrong")
if letter not ["A","B","C","D","F","G"]:
print("Wrong")
if letter == int:
print("Wrong")
if number == str:
print("Wrong")
else:
print("Perfect")
我会用火柴来做这件事:
正则表达式“^[A-H][1-7]$”
是x
必须适合的模式
^ # This character matches the start of the string
[A-H] # After the start of the string we allow the group A-H (A,B,C,D,E,F,G,H)
[1-7] # The next character must be a digit between 1 and 7
$ # This character matches the end of the string
锚的使用意味着x
的长度必须为2,这是隐含的,因此我们不需要对此进行单独检查
循环直到收到正确值的改进:
import re
while not re.match('^[A-H][1-7]$',input("Write something: ")):
print('Wrong')
print('Perfect!')
演示:
我会用火柴来做这件事:
正则表达式“^[A-H][1-7]$”
是x
必须适合的模式
^ # This character matches the start of the string
[A-H] # After the start of the string we allow the group A-H (A,B,C,D,E,F,G,H)
[1-7] # The next character must be a digit between 1 and 7
$ # This character matches the end of the string
锚的使用意味着x
的长度必须为2,这是隐含的,因此我们不需要对此进行单独检查
循环直到收到正确值的改进:
import re
while not re.match('^[A-H][1-7]$',input("Write something: ")):
print('Wrong')
print('Perfect!')
演示:
这是正则表达式的完美用例
import re
x = input("Write something")
if re.match("^[A-H][1-7]$", x):
print("Perfect")
这是正则表达式的完美用例
import re
x = input("Write something")
if re.match("^[A-H][1-7]$", x):
print("Perfect")
这不起作用有两个原因。首先,在这种情况下,字母和数字在技术上都是字符串。来自键盘的任何输入都是字符串,即使是数字。其次,=
操作符用于比较两个对象的相等性(“对象”在这里包括字符串、整数甚至类),与询问“这个对象是这个类的实例吗”不同
我同意其他答案,正则表达式最适合您,但如果您只想使用条件来判断字符串是单字符数字还是字母,可以使用字符串方法:
if not letter.isalpha():
print("Wrong")
if not number.isdigit():
print("Wrong")
这不起作用有两个原因。首先,在这种情况下,字母和数字在技术上都是字符串。来自键盘的任何输入都是字符串,即使是数字。其次,=
操作符用于比较两个对象的相等性(“对象”在这里包括字符串、整数甚至类),与询问“这个对象是这个类的实例吗”不同
我同意其他答案,正则表达式最适合您,但如果您只想使用条件来判断字符串是单字符数字还是字母,可以使用字符串方法:
if not letter.isalpha():
print("Wrong")
if not number.isdigit():
print("Wrong")
使用正则表达式的完美场景
import re
x=input("Write something: ")
print("Perfect" if re.match('^[A-H][1-7]$', x) else "Wrong")
使用正则表达式的完美场景
import re
x=input("Write something: ")
print("Perfect" if re.match('^[A-H][1-7]$', x) else "Wrong")
确切的问题是什么?为什么要将其同时标记为Python3和Python2?挑一个。尤其是因为您使用的
输入功能在它们中的工作方式不同。为什么[A-H][1-7]不起作用?嗨!Python3是正确的标记,对此表示抱歉。谢谢你,[A-H][1-7]是一个很好的方法。确切的问题是什么?为什么你要将它同时标记为Python3和Python2?挑一个。尤其是因为您使用的输入功能在它们中的工作方式不同。为什么[A-H][1-7]不起作用?嗨!Python3是正确的标记,对此表示抱歉。谢谢你,[A-H][1-7]是一个很好的方法。这不够严格,例如A1junk
是一个匹配项。你们也需要在绳子的开始和结束处锚定。说得好,@sudo_O-修正了我的答案!这不够严格,例如A1junk
是匹配项。你们也需要在绳子的开始和结束处锚定。说得好,@sudo_O-修正了我的答案!谢谢我同意你的观点@Andrew Gorcester谢谢!,我同意你的观点@Andrew Gorcesterhi,我是个初学者,为什么我需要导入re?是什么?@user1847307,re
是python的内置正则表达式模块。在使用re.match
之前,必须将其加载到当前名称空间中。您好,我是初学者,为什么需要导入re?是什么?@user1847307,re
是python的内置正则表达式模块。在使用re.match
之前,必须将其加载到当前命名空间中。