在小程序(python 3)中测试字符串是否没有特定特征
我正在尝试编写一个程序,要求用户输入一个以句点结尾的全大写字符串。然后,当输入不符合标准时,让它返回响应。我知道如何检查它是否有大写字母这样的特征,但是我不知道如何检查它是否没有特征。这是我尝试使用!=(如中所示,如果字符串不=大写),但它不起作用。谢谢你的意见,我是新来的,如果这是个愚蠢的问题,我很抱歉在小程序(python 3)中测试字符串是否没有特定特征,python,python-3.x,Python,Python 3.x,我正在尝试编写一个程序,要求用户输入一个以句点结尾的全大写字符串。然后,当输入不符合标准时,让它返回响应。我知道如何检查它是否有大写字母这样的特征,但是我不知道如何检查它是否没有特征。这是我尝试使用!=(如中所示,如果字符串不=大写),但它不起作用。谢谢你的意见,我是新来的,如果这是个愚蠢的问题,我很抱歉 """Making sure they use uppercase and end with a period""" s = input("Please enter an upper-ca
"""Making sure they use uppercase and end with a period"""
s = input("Please enter an upper-case string ending with a period: ")
if s.isupper() and s.endswith("."):
print("Input meets both requirements.")
elif s!=upper():
print ("Input is not all upper case.")
elif s!=endswith("."):
print ("Input does not end with a period.")
else :
print ("You just don't want to do this, do you?")
您不想检查它是否不等于大写字母,因为“大写字母”是一个抽象概念,不能等于任何特定的字符串。您要检查
isupper()
是否为true。只需执行elif not s.upper()
然后elif not s.endswith(“.”
”您不想检查它是否不等于大写——这无论如何都没有意义,因为“大写”是一个抽象概念,不能等于任何特定字符串。您要检查isupper()
是否为true。只需执行elif not s.upper()
,然后elif not s.endswith(“.”
”使用not
将反转逻辑语句
不正确
-->错误
非假
-->真
代码如下:
"""Making sure they use uppercase and end with a period"""
s = input("Please enter an upper-case string ending with a period: ")
if s.isupper() and s.endswith("."):
print("Input meets both requirements.")
elif not s.isupper(): #not False -> True
print ("Input is not all upper case.")
elif not s.endswith("."): #not False -> True
print ("Input does not end with a period.")
else :
print ("You just don't want to do this, do you?")
使用
not
将反转逻辑语句
不正确
-->错误
非假
-->真
代码如下:
"""Making sure they use uppercase and end with a period"""
s = input("Please enter an upper-case string ending with a period: ")
if s.isupper() and s.endswith("."):
print("Input meets both requirements.")
elif not s.isupper(): #not False -> True
print ("Input is not all upper case.")
elif not s.endswith("."): #not False -> True
print ("Input does not end with a period.")
else :
print ("You just don't want to do this, do you?")
只是对代码做了一些调整
s = raw_input("Please enter an upper-case string ending with a period: ")
if s.isupper() and s.endswith('.'):
print("Input meets both requirements.")
elif not s.isupper():
print ("Input is not all upper case.")
elif not s.endswith("."):
print ("Input does not end with a period.")
else :
print ("You just don't want to do this, do you?")
检查是否以“.”结尾或不以“.”结尾时,不应使用!=我是接线员。因为,这两个方法isupper()和endswith()都返回布尔值。因此,要检查它是否正确,只需使用not运算符。只需对代码进行一些调整
s = raw_input("Please enter an upper-case string ending with a period: ")
if s.isupper() and s.endswith('.'):
print("Input meets both requirements.")
elif not s.isupper():
print ("Input is not all upper case.")
elif not s.endswith("."):
print ("Input does not end with a period.")
else :
print ("You just don't want to do this, do you?")
检查是否以“.”结尾或不以“.”结尾时,不应使用!=我是接线员。因为,这两个方法isupper()和endswith()都返回布尔值。因此,要检查它是否为True,只需使用not运算符。
elif not s.isupper()
。Not否定了isupper的结果。就这样。谢谢@RedX我现在觉得很傻elif不是s.isupper()
。Not否定了isupper的结果。就是这样。谢谢@RedX,我现在觉得很傻。尝试使用“不等于”似乎很傻,但由于某种原因,我的头被卡在了上面,看不到其他的方式。谢谢你的回复!是啊,尝试使用“不等于”似乎很愚蠢,但出于某种原因,我的头被卡在了上面,看不到不同的方式。谢谢你的回复!感谢您的输入,现在已修复并运行:)感谢您的输入,现在已修复并运行:)