2012-09-08 54 views
2

這裏是我的代碼:「語法錯誤」用我的如果,否則,和艾利芙語句(Python)的

print "Hello, and welcome to the Slightly Interactive Autobiography of Robbie Wood." 
print "Lets get started, shall we? What chapter would you like to read first?" 
chapter = raw_input("Please type either 'Chapter 1', 'Chapter 2' or 'Chapter 3': ") 

if chapter = "Chapter 1": 
    print "Chapter 1" 
    print chapter_one 

else chapter = "Chapter 2": 
    print "Chapter 2" 
    print chapter_two 

elif chapter = "Chapter 3": 
    print "Chapter 3" 
    print chapter_three 

elif: 
    chapters = raw_input("Please type either 'Chapter 1', 'Chapter 2', or 'Chapter 3': ") 

# Variables - Chapters 
chapter_one = "text here..." 
chapter_two = "text here..." 
chapter_three = "text here..." 

這裏是從終端確切的錯誤信息:

Last login: Fri Sep 7 17:22:59 on ttys000 
Robbies-MacBook-Pro:~ robbiewood$ /var/folders/y6/kx37qgbs34124ztdgb4tphs00000gn/T/Cleanup\ At\ Startup/autobiography-368756862.498.py.command ; exit; 
File "/private/var/folders/y6/kx37qgbs34124ztdgb4tphs00000gn/T/Cleanup At Startup/autobiography-368756862.497.py", line 9 
    else chapter = "Chapter 2": 
      ^
SyntaxError: invalid syntax 
logout 

[Process completed] 

可有人請幫我解決這個問題?我是一名初學者的Python編碼員,我正在爲一個學校項目編寫一個「稍微交互的自傳」。

回答

0

用您的if/elif語句替換===

7

第二組應爲elif,最後一個是你的else情況下,和所有的人都應使用==是否相等的比較,而不是=這是一個變量賦值:

# Define these variables *before* you use them... 
# Variables - Chapters 
chapter_one = "text here..." 
chapter_two = "text here..." 
chapter_three = "text here..." 

if chapter == "Chapter 1": 
    print "Chapter 1" 
    print chapter_one 

# This one should be an elif 
elif chapter == "Chapter 2": 
    print "Chapter 2" 
    print chapter_two 

elif chapter == "Chapter 3": 
    print "Chapter 3" 
    print chapter_three 
# And the last one is an else 
else: 
    chapters = raw_input("Please type either 'Chapter 1', 'Chapter 2', or 'Chapter 3': ") 
+0

或許還提到變量'chapter_one','chapter_two','chapter_three'應該被引用之前聲明? – AsheKetchum

0

有兩個問題:

(1)您正在使用賦值運算符(=)而不是布爾比較運算符(在你的if-表述中的表達式中。

您在if中的表述必須評估爲TrueFalse,那麼您不能在其中指派任務。

(2)此外,你不能有一個elifif語句來結束,它應該是

if BooleanExpr: 
    ... 
elif BooleanExpr: 
    ... 
elif BooleanExpr: 
    ... 
else: 
    ... 

也就是說,如果你要使用elif有在if之後和else之前。

查看Python doc re if瞭解更多信息。

1

首先,您使用==檢查兩個事物是否相等,而不僅僅是=,因爲它是保留用於賦值的。其次,你應該改變你的第二個elseelif,你的最後elifelse。另外,您需要在使用之前定義chapter_xxx變量。試試這個:

print "Hello, and welcome to the Slightly Interactive Autobiography of Robbie Wood." 
print "Lets get started, shall we? What chapter would you like to read first?" 
chapter = raw_input("Please type either 'Chapter 1', 'Chapter 2' or 'Chapter 3': ") 

chapter_one = "text here..." 
chapter_two = "text here..." 
chapter_three = "text here..." 

if chapter == "Chapter 1": 
    print "Chapter 1" 
    print chapter_one 

elif chapter == "Chapter 2": 
    print "Chapter 2" 
    print chapter_two 

elif chapter == "Chapter 3": 
    print "Chapter 3" 
    print chapter_three 

else: 
    chapters = raw_input("Please type either 'Chapter 1', 'Chapter 2', or 'Chapter 3': ")