2

これが私のコードです:

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 コーダーで、学校のプロジェクトで「Slightly Interactive Autobiography」をコーディングしています。

4

4 に答える 4

7

2番目のグループは である必要があり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': ")
于 2012-09-08T00:35:25.830 に答える
1

==最初に、単に=割り当て用に予約されているためではなく、を使用して 2 つのものが等しいかどうかを確認します。次に、秒elseelifで変更し、最後elifをで変更する必要がありますelseまた、変数を使用する前に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': ")
于 2012-09-08T00:38:12.083 に答える
0

if/elif ステートメントで に=置き換えます。==

于 2012-09-08T00:35:19.027 に答える
0

2 つの問題があります。

(1) -ステートメントの式で=、ブール比較演算子 ( ) ではなく代入演算子 ( ) を使用しています。==if

if-statementの式はTrueorに評価される必要Falseがあります。代入はできません。

(2) また、ステートメントelifの最後に を付けることはできません。if

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

つまり、使用するelif場合は、 の後でifany の前にする必要がありますelse

詳細については、Python doc re ifを参照してください。

于 2012-09-08T00:35:29.807 に答える