- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图在 Python3 中定义一个函数来评估输入是否满足给定的要求;它们是:长度必须在 3 到 20 个字符之间(含),必须只有字母和撇号以及“-”和空格。
def validateFirstname(firstname):
#Local Variable
hasFirstname = False
Caracterslist = "/'/-"
if (len(firstname) >= 3 and len(firstname) <= 20 and
firstname.isalpha(), firstname.isspace(), (firstname in Caracterslist)):
hasFirstname = True
return hasFirstname
firstname = str(input("Enter your first name: "))
if (validateFirstname(firstname)):
print("Your first name is: ", firstname)
else:
print("The first name you entered ", firstname, "is not valid!")
实际上,即使名字超过 20 个字符并且包含数字,它也会返回 True……
我不明白为什么...
最佳答案
在你的表达中:
if (len(firstname) >= 3 and len(firstname) <= 20 and
firstname.isalpha(), firstname.isspace(), (firstname in Caracterslist)):
以下部分被评估为元组(注意逗号):
(len(firstname) >= 3 and len(firstname) <= 20 and
firstname.isalpha(), firstname.isspace(), (firstname in Caracterslist))
这基本上是以下形式:
(a and b and c, f, g, h)
因此被评估为一个元组。
这个元组,无论其内容如何,都将评估为 True
,例如:
>>> if (False,False,False):
print "Was True"
Was True
发生这种情况是因为 tuple 本身被认为是 True
/False
意义。
在 Python 中,以下值被认为是假的 Python Documentation :
None
False
zero of any numeric type, for example, 0, 0L, 0.0, 0j.
any empty sequence, for example, '', (), [].
any empty mapping, for example, {}.
instances of user-defined classes, if the class defines anonzero() or len() method, when that method returns the integer zero or bool value False. 1
All other values are considered true — so objects of many types are always true.
由于元组不为空(,)
它将被视为True
,因此表达式的计算结果为True
。您需要用适当的 boolean 运算符替换 ,
。这将停止将表达式视为元组,因为它将采用 (a)
您的表达式存在逻辑问题,您需要为您的逻辑计算出正确的条件链。但是,您还必须解决元组创建问题(如上)。适当的逻辑可能是:
#if fistname is not all spaces, and is of correct length, and (is either all alphabetical or contains a character in characterlist)
if not firstname.isspace() and 3 <= len(firstname) <= 20 and (firstname.isalpha() or any(c in firstname for c in Caracterslist)):
return firstname
关于python - 使用 string.isalpha 在 python 中评估需求,始终返回 True,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20157390/
这个问题在这里已经有了答案: C++: Why does space always terminate a string when read? (4 个答案) 关闭 2 年前。 我编写了一个代码,以
我有GLFW key_callback() 并且我正在尝试检测是否按下了字母字符。我用这个功能isalpha() .我注意到如果执行以下代码,某些键(例如 Shift 和 Alt)会被视为字母字符:
#include #include #include #include #include int main() { char userPassword[20]; printf
正在解决一些 C 问题,但遇到了困难。我可以想出另外一两种解决方法,但我想更好地理解为什么我的检查失败了。 #include #include #include #include int ma
我正在尝试使用 for 循环逐个字符地遍历两个字符串,看看它们在哪里相等,在哪里不同,如果是的话,如果两个都是字母,那么我会做一个条件,但如果一个是字母和一个是下划线,然后我做别的事情。循环的第一部分
我从事此工作已经有一段时间了,现有的答案几乎没有帮助。我是编程新手,正在尝试编写我的程序的一个子部分,它试图检查任何给定的输入是否仅由字母组成。 为此,我想到的想法是通过使用一次传递每个字符的循环将整
我有一个 C 代码,其中我在 ctype.h 中使用标准库函数 isalpha(),这是在 Visual Studio 2010-Windows 上。在下面的代码中,如果 char c 是 '£',则
我有一个程序,它使用 isalpha 比较字符串中的每个元素。我输入一个字符串,它调用函数来循环遍历数组并检查所有字符是否都是字母。如果是,则打印 true,否则打印 false。我的问题是我似乎无法
只是为了好玩,我编写了 isalpha 的这个实现,它可以工作,但我有很多警告,你能告诉我如何修复它们,或者我怎样才能以更好的方式做到这一点? 警告: 第6行:指针和整数的比较 第 23 行:传递“指
我正在用 C 语言编写一个程序,其中涉及选择 4 个选项。但是,我无法验证变量 Option 的值是否是 1、2、3 或 4 之外的字母或数字。当我输入字母时,它继续循环 print 语句,但不循环输
我正在尝试使用 isaplha() 函数来检查每个字符串的每个字符,以确定它是否是字母。但由于某种原因它不起作用。尽管 if 语句有参数,程序总是进入 printf() 函数。看起来代码一切正常: #
我正在对我的 C 程序进行一些错误检查,目前正在测试以确保输入数据点是 float/int (本质上不是字符) 为此,我使用了 isalpha 函数。它适用于 100 以下的所有数字,但如果我输入 1
我正在尝试检查给定字符串中的每个字符是否都是字母。如果不是,则用空格替换该字符。到目前为止我所拥有的是: def removePunctuation(txt): for char in txt
有没有办法让 python isalpha 方法理解 scandics?我尝试了以下方法: >>> import locale >>> locale.getlocale() (None, None)
我一直在尝试使用 JavaScript 验证表单的内容。我需要检查“名称”字段是否仅包含字符和空格。如果存在任何其他字符,我想显示警报。这在 C 和 Java 中很容易做到,但我似乎无法在 JavaS
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我正在尝试检测使用了哪些数学运算符,然后将该运算符分配给一个变量。这是我的代码: start = "3x - 2 = 11" op = "+" for m in start: if m.isalp
while(1) { char buff[1000]; printf("Enter the word: "); fgets(buff, 1000
我正在尝试使用 isalpha() 来验证输入并确保它不是字母。我已尝试正确使用它,但我想我可能遗漏了一些东西。 有问题的部分: scanf("%i", &entry); while (entry !
我正在使用 isalpha() 来确定该行是否有字符,它显示“90 1 0”行包含字母字符。 这里是相关代码 bool digitTest(char *test, int arysize) { int
我是一名优秀的程序员,十分优秀!