为什么用Python阅读一行代码时,我不能只看到那行代码的内容?
我在理解为什么用Python阅读一行代码时,我不能只看到那行代码的内容?,python,Python,我在理解next\u块(文件)函数中的correct变量时遇到问题。如果correct变量未索引,程序将无法正常工作。因此,更正[0]。所以我的问题是,如果它没有被索引,为什么它不能正常工作?如果一个整数不可下标,为什么它甚至可以被索引 它使用的文本文件如下所示: An Episode You Can't Refuse On the Run With a Mammal Let's say you turn state's evidence and need to "get on the lamb
next\u块(文件)
函数中的correct
变量时遇到问题。如果correct
变量未索引,程序将无法正常工作。因此,更正[0]
。所以我的问题是,如果它没有被索引,为什么它不能正常工作?如果一个整数不可下标,为什么它甚至可以被索引
它使用的文本文件如下所示:
An Episode You Can't Refuse
On the Run With a Mammal
Let's say you turn state's evidence and need to "get on the lamb." /If you wait too long, what will happen?
You'll end up on the sheep
You'll end up on the cow
You'll end up on the goat
You'll end up on the emu
1
A lamb is just a young sheep.
The Godfather Will Get Down With You Now
Let's say you have an audience with the Godfather of Soul. /How would it be smart to address him?
Mr. Richard
Mr. Domino
Mr. Brown
Mr. Checker
3
James Brown is the Godfather of Soul.
这是代码:
# Trivia Time
# Trivia game that reads a plain text file
def open_file(file_name, mode):
"""Open a file."""
try:
the_file = open(file_name, mode)
except(IOError), e:
print "Unable to open the file", file_name, "Ending program.\n", e
raw_input("\n\nPress enter to exit..")
sys.exit()
else:
return the_file
def next_line(the_file):
"""Return the next line from the trivia file, formatted."""
line = the_file.readline()
line = line.replace("/", "\n")
return line
def next_block(the_file):
"""Return the next block of data from the trivia file."""
category = next_line(the_file)
question = next_line(the_file)
answers = []
for j in range(4):
answers.append(next_line(the_file))
correct = next_line(the_file)
if correct:
correct = correct[0]
explanation = next_line(the_file)
return category, question, answers, correct, explanation
def welcome(title):
"""Welcome the player and get his/her name."""
print "Welcome to Trivia Challenge!\n"
print title, "\n"
def main():
trivia_file = open_file("trivia.txt", "r")
title = next_line(trivia_file)
welcome(title)
score = 0
# get first block
category, question, answers, correct, explanation = next_block(trivia_file)
while category:
# ask a question
print category
print question
for j in range(4):
print j + 1, "-", answers[j]
# get answer
answer = raw_input("What's your answer?: ")
# check answer
if answer == correct:
print "\nRight!",
score = score + 1
else:
print "\nWrong.",
print explanation
print "Score:", score, "\n\n"
# get next block
category, question, answers, correct, explanation = next_block(trivia_file)
trivia_file.close()
print "That was the last question!"
print "Your final score is:", score
main()
raw_input("\n\nPress the enter key to exit.")
在
correct=next\u行(文件)
之后,correct
是一个类似'1\n'
的字符串<代码>更正[0]然后获取一个类似于'1'
的字符串,稍后您可以将其与原始输入的结果进行比较,该原始输入的结果不包括结尾处的\n
。因此,您需要执行[0]
以取出第一个字符
使用.strip()
可能会更好,因为这样一来,它可能适用于非单个字符的答案(如果您将游戏更改为支持10+个答案,或使用不同名称的答案),结果会更加明显,并且会忽略末端的空格,在文件或用户输入中绝对不相关。在correct=next\u行(文件)
之后,correct
是一个类似'1\n'
的字符串<代码>更正[0]
然后获取一个类似于'1'
的字符串,稍后您可以将其与原始输入的结果进行比较,该原始输入的结果不包括结尾处的\n
。因此,您需要执行[0]
以取出第一个字符
使用.strip()
可能会更好,因为这样一来,它可能适用于非单个字符的答案(如果您将游戏更改为支持10+个答案,或使用不同名称的答案),结果会更加明显,并且会忽略末端的空格,在文件或用户输入中绝对不相关的信息。如果使用“correct”而不是“correct[0]”,它以什么方式不起作用?如果使用“correct”而不是“correct[0]”,它以什么方式不起作用?+1作为答案。附加注释:correct
不是该值的好名称;它表示一个布尔值(真/假)<代码>正确答案
将是一个更好的名称。答案+1。附加注释:correct
不是该值的好名称;它表示一个布尔值(真/假)<代码>正确答案将是一个更好的名称。