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 编码器,我正在为一个学校项目编写一个“稍微互动的自传”。

4

4 回答 4

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': ")
于 2012-09-08T00:35:25.830 回答
1

首先,您使用 来检查两件事是否相等==,而不仅仅是=为分配保留。其次,你应该用 改变你的第二个else,用改变你elif的最后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)您在-statement的表达式中使用了赋值运算符 ( =) 而不是布尔比较运算符 ( )。==if

您在if-statement 中的表达式必须评估为Trueor False,您不能在那里进行分配。

(2) 另外,你不能elif在你的 -statement 的末尾有一个if,它应该是

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

即,如果你要使用elif它必须if在 any 之后和之前else

有关更多信息,请参阅Python 文档

于 2012-09-08T00:35:29.807 回答