簡體   English   中英

Python raw_input() 語法錯誤

[英]Python raw_input() Syntax Error

每當我運行這段代碼時,它一直指向raw_input()的“t”並說SyntaxError: invalid syntax. 我做錯什么了嗎?

if key == True:
                when raw_input().lower() = "get":
                print "Sorry, there is nothing more to get here."

正確的語法是

if key == True:
    if raw_input().lower() == "get":
        print "Sorry, there is nothing more to get here."

沒有關鍵字 when

我知道你的意思,但你正在編寫某種偽代碼。 嘗試這個:

if key:
    while True:
        reply = raw_input().lower()
        if reply == "get":
            print "Sorry, there is nothing more to get here."
            break
        # else ...

我省去了== True ,這只是個人喜好,如果您願意,可以使用它沒有錯。

我將用戶輸入的返回字符串存儲到reply ,因為大概您將處理用戶的回復。 我也把它放在一個循環中,你通過使用單詞when而不是if來暗示它。 break立即退出循環。

暫無
暫無

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

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