在算术测验中构建多个模块

时间:2015-01-01 18:55:36

标签: python math python-3.x module structure

我之前看到过如何达到这一点的例子,但程序的结构化给我带来了真正的问题。现在花了3个小时使用def main()并以main()结尾,相反没有封闭模块在模块中,我没有进一步前进。

我很感激一些建议。

import random
import operator
def bquiz():



    def randomCalc():
        ops = {'+':operator.add,
                '-':operator.sub,
                '*':operator.mul,
                '/':operator.truediv}
        num1 = random.randint(0,12)
        num2 = random.randint(1,10)   
        op = random.choice(list(ops.keys()))
        answer = ops.get(op)(num1,num2)
        print('What is {} {} {}?\n'.format(num1, op, num2))
        return answer
    randomCalc()




    def askQuestion():
        answer = randomCalc()
        guess = float(input())
        return guess == answer
    askQuestion()


    def quiz():
        print('Welcome. This is a 10 question math quiz\n')
        score = 0
        for i in range(10):
            correct = askQuestion()
            if correct:
                    score += 1
                    print('Correct!\n')
            else:
                    print('Incorrect!\n')
            return 'Your score was {}/10'.format(score)
    quiz()

bquiz()

2 个答案:

答案 0 :(得分:1)

你并不是那么遥远。您只需要将所有函数放在全局范围内,然后将quiz放入main函数中,如下所示:

import random, operator

def randomCalc():
    ops = {'+':operator.add,
            '-':operator.sub,
            '*':operator.mul,
            '/':operator.truediv}
    num1 = random.randint(0,12)
    num2 = random.randint(1,10)
    op = random.choice(list(ops.keys()))
    answer = ops.get(op)(num1,num2)
    print('What is {} {} {}?\n'.format(num1, op, num2))
    return answer

def askQuestion():
    answer = randomCalc()
    guess = float(input())
    return guess == answer

def quiz():
    print('Welcome. This is a 10 question math quiz\n')
    score = 0
    for i in range(10):
        correct = askQuestion()
        if correct:
                score += 1
                print('Correct!\n')
        else:
                print('Incorrect!\n')
    print('Your score was {}/10'.format(score))

# start the quiz
quiz()

请注意,我还更改了quiz的最后一行,以便打印分数,而不是返回分数。

答案 1 :(得分:0)

有几件事让你在这里。

首先是python模块和Function之间的区别。函数是一系列指令,它接受0或更多输入,并可能返回0或更多输出。

def IsItGood(x):
   return x=='Good'

模块是函数类等的集合,您可以使用import语句在其他python程序中使用它们。在下面的示例中,bquiz,测验等是函数而不是模块。

其次,当你执行一个python函数时,它会顺序运行,沿途执行每一行代码。在您的原始脚本中

def randomCalc():
    ops = {'+':operator.add,
            '-':operator.sub,
            '*':operator.mul,
            '/':operator.truediv}
    num1 = random.randint(0,12)
    num2 = random.randint(1,10)   
    op = random.choice(list(ops.keys()))
    answer = ops.get(op)(num1,num2)
    print('What is {} {} {}?\n'.format(num1, op, num2))
    return answer
randomCalc()

当你执行bquiz函数时,它会在你定义之后立即调用randomCalc,而你只想在你的问题函数中执行它

第三,你这里不需要bquiz。执行python文件时,它将从上到下执行每一步。因此,如果您删除bquiz函数定义和底部对bquiz的调用,它对程序没有影响(但会提高性能。

第四,研究“打印”和“返回”之间的区别。 Print将字符串写入控制台。这是让您的程序与用户交谈的一种方式。另一方面,返回定义函数的返回值。所以

return 'Your score was {}/10'.format(score)

不会在控制台上打印任何内容。

第五,最后,小心缩进。你在测验中的return语句是里面你的for循环。这意味着在第一个问题之后,函数将命中return语句并退出。函数在到达return语句后停止执行。

以下是测验的工作版本。

import random
import operator

#Removed bzquiz, since it didn't do anything
def randomCalc():
    ops = {'+':operator.add,
            '-':operator.sub,
            '*':operator.mul,
            '/':operator.truediv}
    num1 = random.randint(0,12)
    num2 = random.randint(1,10)   
    op = random.choice(list(ops.keys()))
    answer = ops.get(op)(num1,num2)
    print('What is {} {} {}?\n'.format(num1, op, num2))
    return answer
    #Removed randomCalc() call




def askQuestion():
    answer = randomCalc()
    guess = float(input())
    return guess == answer
#Removed askQuestion() call


def quiz():
    print('Welcome. This is a 10 question math quiz\n')
    score = 0
    for i in range(10):
        correct = askQuestion()
        if correct:
                score += 1
                print('Correct!\n')
        else:
                print('Incorrect!\n')
    print 'Your score was {}/10'.format(score)

quiz()

import random
import operator

    def randomCalc():
        ops = {'+':operator.add,
               '-':operator.sub,
               '*':operator.mul,
               '/':operator.truediv}
        num1 = random.randint(0,12)
        num2 = random.randint(1,10)   
        op = random.choice(list(ops.keys()))
        answer = ops.get(op)(num1,num2)
        print('What is {} {} {}?\n'.format(num1, op, num2))
        return answer





    def askQuestion():
        answer = randomCalc()
        guess = float(input())
        return guess == answer



    def quiz():
        print('Welcome. This is a 10 question math quiz\n')
        score = 0
        for i in range(10):
            correct = askQuestion()
            if correct:
                    score += 1
                    print('Correct!\n')
            else:
                    print('Incorrect!\n')
        print 'Your score was {}/10'.format(score)
    quiz()
bquiz()