功能问题和局部变量

时间:2012-03-05 19:08:08

标签: python

我正在学习Python并正在编写Q& A脚本。我为问题做了一个功能。那很顺利。现在我想要一个普通的功能。我想尽可能避免使用全局变量。我知道我的变量重置在顶部...有人可以给我一些指示吗?我知道C / PHP / BASIC,想要掌握这个语言。以下是我的问题功能。

    def q(question, a, b, c, c_answer):
        total,tally=0,0
        print "",question
        print "   a.",str(a)
        print "   b.",str(b)
        print "   c.",str(c)
        u_answer = raw_input()
        if c_answer == "a" and u_answer == "a":
            print "Correct, the answer is A!"
            tally+=1
        elif c_answer == "b" and u_answer == "b":
            print "Correct, the answer is B!"
            tally+=1
        elif c_answer == "c" and u_answer == "c":
            print "Correct, the answer is C!"
            tally+=1
        else:
           print "I am sorry, but the correct answer is",c_answer
        print "\..n"
        total+=1

2 个答案:

答案 0 :(得分:4)

total功能中删除q。相反,如果问题得到正确回答则返回1,否则返回0:

def q(question, a, b, c, c_answer):
    ...
    return tally

num_correct = 0
for question in questions:
    num_correct += q(...)

average = float(num_correct) / len(questions)

如果您不想使用全局变量,只需在函数或类方法中组织代码:

def ask_questions(questions):
    num_correct = 0
    for question in questions:
        num_correct += q(...)
    return num_correct

def report_average(num_correct, num_questions):
    average = float(num_correct) / num_questions
    print(average)

num_correct = ask_questions(questions)
report_average(num_correct, len(questions))

我认为基本的想法是使用return将您需要的值传递给下一个函数。如果要跟踪许多数据,您可以使用类方法。通过使用类,您可以将值存储为实例属性,而不是使用return

class Exam(object):
    def __init__(self, questions):
        self.num_correct = 0
        self.questions = ...

    def q(self, question, a, b, c, c_answer):
        ...
        if correct:
            self.num_correct += 1

    def ask_questions(self):
        for question in self.questions:
            self.q(question)

    def report_average(self):
        average = float(self.num_correct) / len(self.questions)
        print(average)

答案 1 :(得分:0)

由于您只是在学习Python,我将您的代码重新格式化为pythonic。

def q(question, a, b, c, c_answer):
    total, tally = 0, 0
    print "", question
    print "   a. %s" % a
    print "   b. %s" % b
    print "   c. %s" % c
    u_answer = raw_input("your answer? ").strip()
    if c_answer == u_answer:
        print "Correct, the answer is %s!" % u_answer
        tally += 1
    else:
        print "I am sorry, but the correct answer is %s" % c_answer
    print "\n"
    total += 1

要真正回答你的问题:

有两种方法可以在不使用全局级变量的情况下跟踪问题总数和正确答案(实际上是模块级别,但这是一个不同的主题;)。

一个是传递当前总数,根据当前问题重新计算q,然后将其传回:

def q(question, a, b, c, c_answer, total, tally):
    print "", question
    print "   a. %s" % a
    print "   b. %s" % b
    print "   c. %s" % c
    u_answer = raw_input("your answer? ").strip()
    if c_answer == u_answer:
        print "Correct, the answer is %s!" % u_answer
        tally += 1
    else:
        print "I am sorry, but the correct answer is %s" % c_answer
    print "\n"
    total += 1
    return total, tally

然后在您的主程序中,您可以说:

question_pool = (
    ('What is 2 + 2?', 2, 3, 4, 'c'),
    ('What is blue mixed with yellow?', 'green', 'orange', 'pink', 'a'),
    ('How far does light travel in one nanosecond?', '10 mm', '20 cm', '30 m', 'b'),
    )

total, tally = 0, 0
for packet in question_pool:
    question, a, b, c, answer = packet
    total, tally = q(question, a, b, c, answer, total, tally)

print "you answered %d correctly, for a score of %2.0f%%" % (tally, 100.0 * tally / total)

然而,q处理问题会更好,而不用担心跟踪已回答的问题数量以及已提出的问题数量。

因此,不是接受totaltally,而是重新计算并返回totaltallyq现在只返回0答案是错误的,1如果它是正确的:

def q(question, a, b, c, c_answer):
    print "", question
    print "   a. %s" % a
    print "   b. %s" % b
    print "   c. %s" % c
    u_answer = raw_input("your answer? ").strip()
    if c_answer == u_answer:
        print "Correct, the answer is %s!\n" % u_answer
        return 1
    print "I am sorry, but the correct answer is %s" % c_answer
    return 0

,其余代码如下:

question_pool = (
    ('What is 2 + 2?', 2, 3, 4, 'c'),
    ('What is blue mixed with yellow?', 'green', 'orange', 'pink', 'a'),
    ('How far does light travel in one nanosecond?', '10 mm', '20 cm', '30 m', 'b'),
    )

total, tally = 0, 0
for packet in question_pool:
    question, a, b, c, answer = packet
    tally += q(question, a, b, c, answer)
    total += 1

print "you answered %d correctly, for a score of %.0f%%" % (tally, 100.0 * tally / total)