&引用;如果不是列表中的项目”;在Python中不能正常工作?
我有一个简单的程序,检查用户的“密码”,允许他们访问或拒绝他们访问 如果用户键入正确的密码,则打印语句“Login successful!passcode used:”,passcode“,但是,如果密码错误,则只需再次请求他们的密码,而不打印语句“Login successful” 这是为什么?谢谢&引用;如果不是列表中的项目”;在Python中不能正常工作?,python,python-3.x,Python,Python 3.x,我有一个简单的程序,检查用户的“密码”,允许他们访问或拒绝他们访问 如果用户键入正确的密码,则打印语句“Login successful!passcode used:”,passcode“,但是,如果密码错误,则只需再次请求他们的密码,而不打印语句“Login successful” 这是为什么?谢谢 LoginCorrect = 0 lst = ['1234', '2345', '3456', '4567', '5678', '6789'] while LoginCorrect == 0:
LoginCorrect = 0
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
while LoginCorrect == 0:
passcode = input("Please enter your passcode: ")
for item in lst:
if item == passcode:
print("Login successful! Passcode used:", passcode)
LoginCorrect = 1
if not item in lst == False:
print("Login unsuccessful.")
你把事情弄得太复杂了。你不必自己反复查看列表,只需检查
密码是否存在:
LoginCorrect = False
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
while LoginCorrect:
passcode = input("Please enter your passcode: ")
if passcode in lst:
print("Login successful! Passcode used:", passcode)
LoginCorrect = True
else:
print("Login unsuccessful.")
你把事情弄得太复杂了。你不必自己反复查看列表,只需检查密码是否存在:
LoginCorrect = False
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
while LoginCorrect:
passcode = input("Please enter your passcode: ")
if passcode in lst:
print("Login successful! Passcode used:", passcode)
LoginCorrect = True
else:
print("Login unsuccessful.")
我希望这只是一个练习,而不是执行登录的生产代码,因为它是超级不安全的
以下是您的代码段的工作版本:
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
LoginCorrect = False
while not LoginCorrect:
passcode = input("Please enter your passcode: ")
if passcode in lst:
print("Login successful! Passcode used:", passcode)
LoginCorrect = True
else:
print("Login unsuccessful.")
我希望这只是一个练习,而不是执行登录的生产代码,因为它是超级不安全的
以下是您的代码段的工作版本:
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
LoginCorrect = False
while not LoginCorrect:
passcode = input("Please enter your passcode: ")
if passcode in lst:
print("Login successful! Passcode used:", passcode)
LoginCorrect = True
else:
print("Login unsuccessful.")
要回答您的具体问题:lst中的not项
工作正常,但
lst==False中的not项的计算方式可能与您预期的不同。
Python转换逻辑测试链,如(a==b==c)
或(a在b中在c中)
到(a==b)和(b==c)
或(b中的a)和(c中的b)
。当
在同一表达式中查看和=
。因此,lst==False中的非项计算为
not((lst中的项)和(lst==False))
,它总是True
(没有not
,它
将始终为False
,这可能是您最初提出问题的原因)
您可以通过在第一个测试周围添加括号来解决此问题:(不是lst中的项)=False
(或者您的意思是True
?)。但是如果项不在lst:
,编写此测试的更好方法是。
您的代码的组织还存在一些其他问题
我在下面的评论中指出:
LoginCorrect = 0 # probably better to use True/False here
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
while LoginCorrect == 0:
# The next line will not work right on Python 2.x, which has a different
# input() function; if your code might run on 2.x you should adjust for that.
passcode = input("Please enter your passcode: ")
for item in lst:
if item == passcode:
print("Login successful! Passcode used:", passcode)
LoginCorrect = 1
# you could use a 'break' here to avoid comparing to more items
# As noted above, `if item not in lst:` would work better for the next line.
# This test should also probably occur after the `for` loop instead of
# inside it, and act based on `LoginCorrect` instead of `item`. As it is,
# you are testing whether `item` is in `lst`, which it always is, and you
# are doing this test once for each `item`, which is more checks than needed.
if not item in lst == False:
print("Login unsuccessful.")
您在对不同答案的评论中提到,您必须使用for
循环
针对每个项目测试密码。如果是真的,那么下面的修改代码
可以很好地工作:
try:
# Python 2 compatibility
input = raw_input
except NameError:
pass
LoginCorrect = False
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
while not LoginCorrect:
passcode = input("Please enter your passcode: ")
for item in lst:
if item == passcode:
print("Login successful! Passcode used:", passcode)
LoginCorrect = True
break
if not LoginCorrect:
print("Login unsuccessful.")
或者,如果您愿意放弃
for
循环,您可以使代码更简单:
try:
# Python 2 compatibility
input = raw_input
except NameError:
pass
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
while True:
passcode = input("Please enter your passcode: ")
if passcode in lst:
print("Login successful! Passcode used:", passcode)
break
else:
print("Login unsuccessful.")
要回答您的具体问题:lst中的not项
工作正常,但
lst==False中的not项的计算方式可能与您预期的不同。
Python转换逻辑测试链,如(a==b==c)
或(a在b中在c中)
到(a==b)和(b==c)
或(b中的a)和(c中的b)
。当
在同一表达式中查看和=
。因此,lst==False中的非项计算为
not((lst中的项)和(lst==False))
,它总是True
(没有not
,它
将始终为False
,这可能是您最初提出问题的原因)
您可以通过在第一个测试周围添加括号来解决此问题:(不是lst中的项)=False
(或者您的意思是True
?)。但是如果项不在lst:
,编写此测试的更好方法是。
您的代码的组织还存在一些其他问题
我在下面的评论中指出:
LoginCorrect = 0 # probably better to use True/False here
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
while LoginCorrect == 0:
# The next line will not work right on Python 2.x, which has a different
# input() function; if your code might run on 2.x you should adjust for that.
passcode = input("Please enter your passcode: ")
for item in lst:
if item == passcode:
print("Login successful! Passcode used:", passcode)
LoginCorrect = 1
# you could use a 'break' here to avoid comparing to more items
# As noted above, `if item not in lst:` would work better for the next line.
# This test should also probably occur after the `for` loop instead of
# inside it, and act based on `LoginCorrect` instead of `item`. As it is,
# you are testing whether `item` is in `lst`, which it always is, and you
# are doing this test once for each `item`, which is more checks than needed.
if not item in lst == False:
print("Login unsuccessful.")
您在对不同答案的评论中提到,您必须使用for
循环
针对每个项目测试密码。如果是真的,那么下面的修改代码
可以很好地工作:
try:
# Python 2 compatibility
input = raw_input
except NameError:
pass
LoginCorrect = False
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
while not LoginCorrect:
passcode = input("Please enter your passcode: ")
for item in lst:
if item == passcode:
print("Login successful! Passcode used:", passcode)
LoginCorrect = True
break
if not LoginCorrect:
print("Login unsuccessful.")
或者,如果您愿意放弃
for
循环,您可以使代码更简单:
try:
# Python 2 compatibility
input = raw_input
except NameError:
pass
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
while True:
passcode = input("Please enter your passcode: ")
if passcode in lst:
print("Login successful! Passcode used:", passcode)
break
else:
print("Login unsuccessful.")
用continue
和break
的while
语句怎么样
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
while True:
passcode = input('Please enter your passcode: ')
if passcode not in lst:
continue
else:
print("Login successful! Passcode used:", passcode)
break
您声明“如果密码错误,它只需再次请求他们的密码,而不打印“登录不成功”语句。”
既然如此,为什么要为“登录不成功”编码呢?用继续和中断的while
语句怎么样
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
while True:
passcode = input('Please enter your passcode: ')
if passcode not in lst:
continue
else:
print("Login successful! Passcode used:", passcode)
break
您声明“如果密码错误,它只需再次请求他们的密码,而不打印“登录不成功”语句。”
既然如此,您为什么要编写“登录不成功”的代码?代码的逻辑有些不清楚。我想您是想这样做的:
LoginCorrect = 0
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
while LoginCorrect == 0:
passcode = input("Please enter your passcode: ")
for item in lst:
if item == passcode:
print("Login successful! Passcode used:", passcode)
LoginCorrect = 1
break
if passcode not in lst:
print("Login unsuccessful.")
顺便说一句,在Python中使用布尔值False
和True
作为LoginCorrect
标志更为常见,例如
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
LoginCorrect = False
while not LoginCorrect:
passcode = input("Please enter your passcode: ")
for item in lst:
if item == passcode:
print("Login successful! Passcode used:", passcode)
LoginCorrect = True
break
if passcode not in lst:
print("Login unsuccessful.")
然而,正如其他人所说,使用Python循环来测试passcode
是否在lst
中是低效的。in
测试更有效,如果lst
是有效密码的集,则效率更高
lst = {'1234', '2345', '3456', '4567', '5678', '6789'}
while True:
passcode = input("Please enter your passcode: ")
if passcode in lst:
print("Login successful! Passcode used:", passcode)
break
print("Login unsuccessful.")
我必须提到的是,代码中的if
语句之一并没有达到您认为的效果:
if not item in lst == False:
为了解释原因,我需要稍微绕道。:)
Python语法支持关系运算符的链接
if 0 <= a < 5:
如果bool(left\u expression)
为真,它只计算right\u expression
。因此,在这些链式测试中,Python从左到右运行,如果在任何阶段检测到错误结果,它就会断开链式
此外,在链式测试中,任何中间表达式都保证最多计算一次。例如,在f()
中,函数g
最多调用一次,并且只有在f()
返回非错误结果时才会调用它
现在,回到您的代码:)
操作符中的是一个关系操作符,因此可以在链式测试中使用
if item in lst == True:
是马
ab = ('a', 'b')
print('b' in ab in [('a', 'b'), 'c'])