我如何添加函数来打印用户分数的百分比以及用户的正确答案?

2024-10-03 17:18:07 发布

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

现在它只是打印了不正确的问题,我不知道如何让它也打印用户的百分比

# -*- coding: utf-8 -*-
def main():
correctAnswers = ['B', 'D', 'C', 'B', 'C', 'D', 'A', 'B', 'D', 'A']
# questionNumber = ['1′,'2′,'3′,'4′,'5′,'6′,'7′,'8′,'9′,'10']

studentInput = raw_input('Enter the Answers in one line: ')
studentInput = studentInput.split(' ')
studentAnswerList = studentInput
print studentAnswerList

correct = 0
incorrect = 0
incorrectAnswers = [0] * 10
for count in range(10):
    if studentAnswerList[count] == correctAnswers[count]:
        correct += 1
    elif studentAnswerList[count] != correctAnswers[count]:
        incorrect += 1
        incorrectAnswers[count] = studentAnswerList[count]
print studentAnswerList
print correctAnswers

# if statement to compare answers and indicate if student passed or failed
if correct >= 5:
    print 'Passed ', correct
else:
    print 'Failed ', incorrect
print incorrectAnswers

main()

Tags: 用户inifmaincountutf百分比print
1条回答
网友
1楼 · 发布于 2024-10-03 17:18:07

我修改了您的代码,在启动程序之前添加了一点比较,并添加了百分比。看来这就是你想要的

from __future__ import division
def main():
    correctAnswers = ['B', 'D', 'C', 'B', 'C', 'D', 'A', 'B', 'D', 'A']

    studentInput = raw_input('Enter the Answers in one line: ')
    studentInput = studentInput.split(' ')
    studentAnswerList = studentInput
    print studentAnswerList

    correct = 0
    incorrect = 0
    incorrectAnswers = [0] * 10
    percentage = 0.0

    if len(studentAnswerList) == len(correctAnswers):
        for count in range(10):
            if studentAnswerList[count] == correctAnswers[count]:
                correct += 1
            elif studentAnswerList[count] != correctAnswers[count]:
                incorrect += 1
                incorrectAnswers[count] = studentAnswerList[count]

        if correct > 0:
            percentage = (incorrect / len(correctAnswers))*100

        # if statement to compare answers and indicate if student passed or failed
        if correct >= 5:
            print 'Passed ', correct
        else:
            print 'Failed ', incorrect

        print 'You have failed the ' + str(percentage) + '% questions'
    else:
        print 'You should answer to ' + str(len(correctAnswers)) + ' questions'

main()

相关问题 更多 >