用Python调试Trivia游戏

时间:2011-05-24 14:37:22

标签: python debugging text-files

创建打印问题的功能,将选项添加到列表中。

def print_et_list ():
    answer_list = []
    function = open ("modStory.txt","r")
    #Question
    question = function.readline()
    print question
    #Choices
    one = answer_list.append (function.readline())
    two = answer_list.append (function.readline())
    for item in answer_list:
        print item
    #Solution
    try:
        solution = int(function.readline())
    except:
        print "There's an error in the answer"

    ##for the blank line
    function.readline()


    return question, one, two, solution, function

  ##Function for prompting the user for an answer, comparing an answer, keeping score        and printing score.
def hey_user (solution):
    score = 0
    user_answer = int(raw_input ("Enter what you think the answer is, user.\n"))
    if user_answer == solution:
        print "You've got it right!"
        score = score + 1
    elif user_answer == 0:
        sys.exit()
    else:
        print "You've got it wrong."
    return score

def main ():
        question, one, two, solution, function = print_et_list()
        scoresofar = hey_user (solution)
        print "\nYour score is now", scoresofar
        while question:
            question, one, two, solution, function = print_et_list()
        function.close()

main ()


raw_input ("Hit enter to exit.")

出于某种原因,我无法正确循环这个东西。 infinte上面的代码循环自己。 以下是有问题的文本文件,它只是乱码的歌词。程序将正确运行第一个片段,并在用户给出答案后将infinte循环第一个片段。

Can you carry my drink I have everything else
1 - I can tie my tie all by myself
2 - I'm getting tired, I'm forgetting why
2

is diving diving diving diving off the balcony
1 - Tired and wired we ruin too easy
2 - sleep in our clothes and wait for winter to leave
1

While it sings to itself or whatever it does
1 - when it sings to itself of its long lost loves
2 - I'm getting tired, I'm forgetting why
2

2 个答案:

答案 0 :(得分:2)

要纠正无限循环,请避免在每次调用print_et_list()

时重新打开文件

尝试此操作(我将function重命名为file_handle,以便在阅读代码时更加明确一些)

import sys

def print_et_list (file_handle):
    answer_list = []
    #Question
    question = file_handle.readline()
    print question
    #Choices
    one = file_handle.readline()
    two = file_handle.readline()
    answer_list.append(one)
    answer_list.append (two)
    for item in answer_list:
        print item
    #Solution
    solution = None
    try:
        result = file_handle.readline()
        result.replace("\n","")
        solution = int(result)
    except:
        print "There's an error in the answer"

    ##for the blank line
    file_handle.readline()
    return question, one, two, solution

  ##file_handle for prompting the user for an answer, comparing an answer, keeping score        and printing score.
def hey_user (solution, score=0):
    user_answer = int(raw_input ("Enter what you think the answer is, user.\n"))
    print "you answered '%s'"%user_answer
    if user_answer == solution:
        print "You've got it right!"
        score += 1
    elif user_answer == 0:
        sys.exit()
    else:
        print "You've got it wrong."
    return score

def main ():
        file_handle = open ("modStory.txt","r")
        question, one, two, solution = print_et_list(file_handle)
        scoresofar = hey_user(solution)
        print "\nYour score is now", scoresofar
        while question:
            question, one, two, solution = print_et_list(file_handle)
            if question:
                scoresofar = hey_user(solution, scoresofar)
                print "\nYour score is now", scoresofar
        file_handle.close()

main ()


raw_input ("Hit enter to exit.")

这不是一个完美的版本,但似乎有效;)

答案 1 :(得分:0)

append不返回任何内容,因此onetwoNone