2014-09-28 193 views
0

IDLE正在將if語句註冊爲語法錯誤。我的代碼如下:if語句中的IDLE語法錯誤

import random ;\ 
    print("Welcome to the fortune cookie simulator") ;\ 
    print("\n\nThe fortune cookie minus the good part..") ;\ 
    input("\n\n\nPress enter to recieve your fortune!") ;\ 
    fortune = random.randint(1, 5) ;\ 
    if fortune==1: ;\ 
     print("You will die today") ;\ 
+3

爲什麼你有; \在每一行的結尾? – 2014-09-28 20:50:42

+0

這不是Python語法......或者說,我應該說,這是一個單線程。 – Tritium21 2014-09-28 20:52:09

+0

將多條線段分開......有趣。 – chepner 2014-09-28 20:53:45

回答

0

您使用顯式語句分隔符和行連續創建了一個大的oneliner。它的醜陋和錯誤傾向於這樣工作。我假設你正在使用python3。在python2中,input()需要用戶輸入一個有效的python表達式。如果您使用的是python2,請改用raw_input()。

import random 
print("Welcome to the fortune cookie simulator") 
print("\n\nThe fortune cookie minus the good part..") 
input("\n\n\nPress enter to recieve your fortune!") 
fortune = random.randint(1, 5) 
if fortune == 1: 
    print("You will die today") 
1

由於if fortune==1:不是一個完整的語句,你不能用;終止它。一個if語句的正確的線形態僅僅是

if fortune==1: print("...") 

拆分到這兩條線的話,則只需使用普通的Python

if fortune==1: 
    print("...") 

爲什麼你正在試圖將多個語句組合成一個邏輯線是另一個問題。

+0

Python不適合單行者。讓我們不要鼓勵它? – Tritium21 2014-09-28 20:57:01

+0

哦,我不是。我只想指出兩個錯誤(使用';'結合兩行,然後使用'\'將行再次分成兩行)產生了一個更大的錯誤。 – chepner 2014-09-28 20:59:48