2012-04-22 49 views
0

我無法理解next_block(the_file)函數中的correct變量。如果correct變量未編入索引,程序將無法正常工作。因此,correct[0]。所以我的問題是爲什麼它不能正確工作,如果它沒有索引,並且爲什麼它可以索引,如果一個整數是不可下標的。爲什麼在Python中閱讀一行不會給我這行內容?

它使用純文本文件是這樣的:

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. 

這是代碼:

correct = next_line(the_file)
# 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.") 
+0

如果您使用「正確」而不是「正確[0]」,它以何種方式失敗? – 2012-04-22 00:54:19

回答

6

後,correct就像'1\n'一個字符串。 correct[0]然後得到一個像'1'這樣的字符串,您稍後將其與raw_input的結果進行比較,該結果在末尾不包括\n。所以你需要做[0]以獲得第一個字符。

它可能會更好地使用.strip()來代替,因爲那樣它可能適用於不是單個字符的答案(如果您將遊戲更改爲支持10多個答案或使用不同類型的名稱進行答案) ,它會更明顯地發生什麼,它會忽略兩端的空格,這在文件或用戶的輸入中絕對不相關。

+0

哈哈,忘了那個。謝謝你,你真棒。 – 2012-04-22 01:16:35

+0

小指餡餅:不要忘記接受答案 – 2012-04-22 01:35:55

+2

+1的答案。額外的評論:'正確'不是這個值的好名字;它建議一個布爾值(真/假)。 'correct_answer'將是一個更好的名字。 – 2012-04-22 01:46:06

相關問題