我的导师让我做一个'ID printer',我想让程序在输入你的名字时不接受整数,但是这样做不会接受字符串。我的代码在下面。在
User_Name = ""
def namechecker():
print("Please Input Your name")
User_Name = str(input(":"))
while User_Name == "":
print("Please input your name")
User_Name = str(input(":"))
while User_Name != str():
print("Please use characters only")
print("Please input your name")
User_Name = input (":")
print("Thankyou, ", User_Name)
namechecker()
如果您想跟上检查数字的想法,还可以使用str.isdigit公司()
像这样:
请注意,如果给定的字符串只包含个数字,这个代码才会请求另一个输入。如果要确保字符串只由字母字符组成,则可以使用字符串.isalpha()
^{pr2}$这样就可以了,而且输入中不允许有数字。但是,您应该阅读Built-in Types上的文档。在
你的问题很不清楚。在仔细阅读之后,我认为您希望获得一个只有字母字符的用户名。您可以使用
str.isalpha
来完成此操作:相关问题 更多 >
编程相关推荐