Python数学测验

2024-05-15 22:44:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python创建一个数学测验,但是我遇到了一些困难,如果有人能帮助我,我将不胜感激。 我需要程序问用户10个问题,然后计算出10个用户的得分。但是我的程序没有这样做,而是问了12个问题,没有计算用户的分数。

这是到目前为止我的代码的复制和粘贴:

import random
import operator

def quiz():

    print('Welcome. This is a 10 question math quiz\n')
    name = input("Please enter your name")
    print("Hello", name," Let's begin the quiz!")
    score = 0
    for i in range(10):
        correct = askQuestion()
        if correct:
            score += 1
            print('Correct!\n')
            print(score)
            break
        else:
            print('Incorrect!\n')

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


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

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

quiz()
askQuestion()
randomCalc()

Tags: 用户answernamereturndefrandomquizoperator
3条回答

代码片段中存在一些逻辑和表示错误,您面临的主要问题是得到12个问题,而不是10个,这是因为您在代码末尾调用askQuestion()randomCalc(),同时从quiz()函数本身内部调用它们。另一个问题是在循环中使用break语句,我猜您混淆了continuebreak语句,break用于退出循环,但是对于Python for循环,您不需要任何break/continue构造。提供的代码可能看起来像

import random
import operator

def quiz():

    print('Welcome. This is a 10 question math quiz\n')
    name = input("Please enter your name")
    print("Hello", name," Let's begin the quiz!")
    score = 0
    for i in range(10):
        correct = askQuestion()
        if correct:
            score += 1
            print('Correct!')
            print "Score",(score),"\n"
        else:
            print('Incorrect!')
            print "Score",(score), "\n"

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


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

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

quiz()
#askQuestion()
#randomCalc()

我在输出中更改了一些内容,您应该使用.format作为变量,而不是其他任何东西。

现在应该可以了。。。

import random
import operator

    def quiz():

    print('Welcome. This is a 10 question math quiz\n')
    name = input("Please enter your name\n")
    print("Hello", name,"\n Let's begin the quiz!")
    score = 0
    for i in range(10):
        correct = askQuestion()
        if correct:
            score = score + 1
            print('Correct!')
            print ("Score is {}".format(score))
        else:
            print('Incorrect!')
            print ("Score is {}".format(score))

     print("{}, your score was {}/10".format(name, score))


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

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

quiz()
#askQuestion()
#randomCalc()

这可能是不正常的,因为(在写这篇文章的时候)你的缩进没有正确通过,但是我相信你需要改变这个:

quiz()
askQuestion()
randomCalc()

对此:

print(quiz())

正如你现在所拥有的,“你的分数是…”永远不会被打印出来,因为你returnquiz结尾的字符串,但是你永远不会对结果做任何处理。你得到的是12个问题而不是10个,因为你不必要在打电话给askQuestion之后再打电话给randomCalc

相关问题 更多 >