Python Else分支机构不是';不执行
我有以下代码,但是Python Else分支机构不是';不执行,python,python-3.x,Python,Python 3.x,我有以下代码,但是else分支不起作用 def main(): while True: username = input ("Enter Username: ") password = input ("Enter Password: ") if username == 'Filip' and password == 'XD' or "Miroslav" and "plusko12": import time
else
分支不起作用
def main():
while True:
username = input ("Enter Username: ")
password = input ("Enter Password: ")
if username == 'Filip' and password == 'XD' or "Miroslav" and "plusko12":
import time
time.sleep(1)
print ("Login successful!")
logged()
else:
print("STOP")
def logged():
import time
time.sleep(1)
print ("Welcome to the Server")
#Booting now
print("Booting will begin shortly")
import time
time.sleep(3)
print("Starting.................0%")
# ... and there's more stuff in here
quit(0)
main()
有两件事“行不通”:
不会退出,因为没有停止条件
然后下一个如果:
if username == 'Filip' and password == 'XD' or "Miroslav" and "plusko12":
将始终计算为True,因为任何非空字符串都为True,其计算方式如下:
if (username == 'Filip' and password == 'XD') or ("Miroslav" and "plusko12")
# |---this is always True---|
由于或操作数之一始终为真,因此它将始终进入分支
还有一个更一般的提示:如果您在顶部导入时间而不进行缩进,则不需要不断地重新导入时间(大多数都是不必要的)。在这种情况下,“不工作”是什么意思?顺便问一下,试试if(username=='Filip'和password=='XD')或(username=='Miroslav'和password=='plusko12”):
你的意思是和密码在{'XD',“Miroslav”,“plusko12}
中吗?我认为缩进也不正确,我怀疑if
应该在中,而中。
if (username == 'Filip' and password == 'XD') or ("Miroslav" and "plusko12")
# |---this is always True---|