簡體   English   中英

我的程序在Python shell中運行,但在cmd.exe中運行時無法識別我的輸入

[英]My program works in Python shell but doesn't recognize my input when it runs in cmd.exe

嘿,我對此完全陌生,不知道為什么我什至試圖在cmd.exe中運行我的代碼...

但是,當我在其中運行腳本時,當我輸入正確的輸入密碼時無法識別。 當我在python shell中運行它時,它工作正常。

任何將我指向正確學習方向的答案或幫助都會很棒。

secret_info=["Whatmough","Graham","NOOB"]
password="1234"
tries=0
locked = 1

def cracker():
    attempt=input("Type your Password: ")

    return attempt

def checker():
    global locked, tries
    if cracker() == password:
         locked = 0
    else:
        tries = tries + 1


while 3 > tries and locked==1:
    checker()
    if locked == 0:
        print(secret_info)
    if tries == 3:
        secret_info=0
        print("Self Distructed! Your secret info is now:", secret_info)

原因是cmd將回車符( '\\r' )添加到輸入字符串。

如果嘗試打印repr(attempt)則可以看到"\\r"

要刪除該回車,可以使用str.strip()str.rstrip("\\r")

attempt = input("Type your Password: ").rstrip("\r")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM