Python计算成绩

时间:2013-10-05 02:14:46

标签: python

missing = 0
highscore = 0
inclass = 0
takenexam = 0
count = 0
total = 0

minGrade = 100 #assuming 100 is the highest grade possible.
maxGrade = 0

score = int(input("Enter a score (-1 to quit): "))

while score > -1 :
    if score >= 80 :
        highscore = highscore + 1
    if score == 0 :
        missing = missing + 1
    if 0 <= score <= 100 :
        inclass = inclass + 1
    if 0 < score <= 100 :
        takenexam = takenexam + 1
    # Determine if the score is the min or max score.
    if score < minGrade :
        minGrade = score
    if score > maxGrade :
        maxGrade = score
    # Add the grade to the running total
    total = total + score
    count = count + 1

    # Read the next grade.
    score = int(input("Enter a score (-1 to quit): "))

# Print the results.
if count > 0 :
    average = total / count
print("number of students in the class: ", inclass)
print("number of students who missed the exam: ", missing)
print("number of students who took the exam: ", takenexam)
print("number of students who scored high: ", highscore)
print("The average of all students in the class %.2f" % average)

这就是我目前正在编码的方式,我现在很好奇我如何能够帮助这个满足句子控制的循环,我还能如何增加参加考试的所有学生的平均分数?

4 个答案:

答案 0 :(得分:0)

您似乎想通过缩进将以下块放在while循环中。

# Add the grade to the running total
total = total + score
count = count + 1

# Read the next grade.
score = int(input("Enter a score (-1 to quit): "))

答案 1 :(得分:0)

试试这段代码。你的逻辑几乎没问题,我不得不在几个地方清理缩进,打印语句和一些变量名称(你使用max Grade而不是maxGrademaxGrad而不是{{1等等)

maxGrade

答案 2 :(得分:0)

这是对代码的快速修改......比如ansh0l,我把它清理了一下。我没有通过优化任何东西为你做功课,也没有为改进你的错误捕获代码做出任何努力(也就是如果你不输入数字会发生什么?---错误!)。但是,试一试。这似乎对我有用,我能让你的print工作。

当心!!!我使用python 2.7,你使用python 3. *。您可能需要删除顶部的import语句才能使其正常工作。

from __future__ import print_function

#current code
missing = 0
highscore = 0
inclass = 0
takenexam = 0
count = 0
total = 0
## Setting the initial score so the 'while' condition kicks-off.
score = 0 
minGrade = 100 #assuming 100 is the highest grade possible.
maxGrade = 0

while score > -1 :
    score = int(input("Enter a score (-1 to quit): "))
    if score == -1:
        break
    elif score >= 80 :
        highscore = highscore + 1
    elif score == 0 :
        missing = missing + 1
    else:
        ## Just being explicit!
        pass


    if 0 <= score <= 100 :
        inclass = inclass + 1
        takenexam = takenexam + 1
    else:
        ## Just being explicit!
        pass

# Determine if the score is the min or max score.
    if score < minGrade :
        minGrade = score
    if score > maxGrade :
        maxGrade = score

    # Add the grade to the running total
    total = total + score
    count = count + 1

# Print the results.
if count > 0 :
    average = total / count


print("number of students in the class: ", inclass)
print("number of students who missed the exam: ", missing)
print("number of students who took the exam: ", takenexam)
print("number of students who scored high: ", highscore)
print("The average grade is %.2f" % average)

答案 3 :(得分:0)

使用delim,确保使用while循环设置它。

EX:

While X != -1.....

如果值为-1,则可以使用某种退出代码。但通常这是结束你的while循环的delim,否则你可以选择继续添加值。另一方面,平均值取自所有总计得分的总量。

EX: 一名学生参加了一次考试,当他没有接受另一名考试时,他们就参加了考试。 100 + 0(totalScores) = 100 / 2(exams taken) = .5,也称为50%。为考试设置一个计数变量,为所有考试添加另一个计分变量。这可以帮助您找到平均值。祝你好运!