2017-04-11 34 views
-2

I次のコードを持っていますが、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() 
+1

この文脈で「機能しない」とはどういう意味ですか?ちなみに 'if(username == 'Filip'とpassword == 'XD')か(username ==" Miroslav "とパスワード==" plusko12 "):' –

+1

' XD '、 "Miroslav"、 "plusko12"}? –

+1

インデントも間違っていると思います。 'if'が' while'の内側にあるはずです。 – MSeifert

答えて

2

「動作しない」という二つのものがあります。

while True: 
    username = input ("Enter Username: ") 
    password = input ("Enter Password: ") 

ますが、停止条件がないので終了しないでください。その後、次のif

if username == 'Filip' and password == 'XD' or "Miroslav" and "plusko12": 
任意のない空の文字列がTrueで、それは次のように評価されているため、常に真と評価されます

if (username == 'Filip' and password == 'XD') or ("Miroslav" and "plusko12") 
#            |---this is always True---| 

orオペランドのいずれかが常にあるので、本当それは常に枝に入るでしょう。

また、より一般的なヒント:インデントなしで先頭にimport timeがある場合は、常にimportを再入力する必要はありません(そのほとんどはそのままです)。

関連する問題