如果两个列表中的任何元素相等,我该如何编写错误消息?
If any element in the two lists is equal how can i write a error message?
示例我有 2 个列表。
import string
character1= list(string.punctuation)
password=['a','b','1','@']
if(character1 in password):
print("Error! Because you have a special character/characters in your password.Try again")
else:
pass
如何创建这样的程序?它在 jupyter 中不起作用。感谢您的帮助!
目前,您代码中的 character1
是一个列表,您正在测试此列表是否在 password
列表中。您需要测试 character1
中是否存在 password
中的元素。一种方法是使用 any()
Python built-in 函数 (documentation)。 any()
接受一个可迭代对象(在您的例子中是一个列表)和“returns True
如果可迭代对象的任何元素为真”作为参数。下面,这个可迭代参数是生成器 (i for password for i in character1)
.
if any(i in password for i in character1):
print("Error! Because you have a special character/characters in your password.Try again")
else:
pass
示例我有 2 个列表。
import string
character1= list(string.punctuation)
password=['a','b','1','@']
if(character1 in password):
print("Error! Because you have a special character/characters in your password.Try again")
else:
pass
如何创建这样的程序?它在 jupyter 中不起作用。感谢您的帮助!
目前,您代码中的 character1
是一个列表,您正在测试此列表是否在 password
列表中。您需要测试 character1
中是否存在 password
中的元素。一种方法是使用 any()
Python built-in 函数 (documentation)。 any()
接受一个可迭代对象(在您的例子中是一个列表)和“returns True
如果可迭代对象的任何元素为真”作为参数。下面,这个可迭代参数是生成器 (i for password for i in character1)
.
if any(i in password for i in character1):
print("Error! Because you have a special character/characters in your password.Try again")
else:
pass