-3

我正在使用 Python 3.2 只是为了让您知道我在做什么,这是分配: ~ random 模块中的函数 random.randint 可用于从一系列值中生成整数。例如,random.randint(1,6) 以相等的概率产生值 1 到 6。一个简单的辅导程序可能会随机选择 0 到 12 之间的两个数字并提出如下问题:6 乘以 8 是多少?收到用户回复后,计算机会检查答案是否正确并给出鼓励性的评论。编写一个程序,循环 10 次产生这种形式的问题,并在最后给用户打分。

这是我的程序中的内容:

    print ("Hello. Let's begin")
    for i in range (1,10):
        from random import randint
        x=randint (0,12)
        y=randint (0,12)
        print (x,"*" y,"=?")
        product= int(input ("What is the product?")
        if (product==x*y):
           print ("Awesome! That is correct!")
        else:
           print ("Sorry, that is not correct, but let's try another one!")

我拥有所有这一切。它向用户询问一个随机乘法问题并回答十次。我不明白该怎么做是最后给用户一个分数。我正在集思广益,但真正奏效的并不多。我想我必须做类似的事情:

    score=

但我不知道如何告诉程序计算正确答案的数量......我说 score=number of if 吗?

然后当我打印分数时,我可以说:

    if (score>5) :
       print: ("Great job! You scored a",score,"out of ten!")
    else:
       print: ("Not the best score, but you can try again! You scored a",score,"out of ten.")

或者有没有更简单的方法来做到这一点?

4

2 回答 2

1

似乎最简单的方法是创建一个新变量(“score”或类似的)并在循环之前将其初始化为 0。然后,当您检查用户是否正确时,如果正确则将其加一,如果错误则不理会它。

希望这可以帮助!

于 2012-12-05T17:44:43.780 回答
0

首先,将分数设置为 0

score = 0

然后在循环中,尝试类似

    if (product==x*y):
       print ("Awesome! That is correct!")
       score += 1
    else:
       print ("Sorry, that is not correct, but let's try another one!")

重要的是,score += 1当您得到正确答案时,分数会增加一分。您可以在score > 5循环之后放入。

于 2012-12-05T17:51:53.150 回答