Python计算成绩

2024-10-01 13:45:42 发布

您现在位置: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)

这就是我目前的编码方式,我现在很好奇,我如何能帮助这个满足一个句子控制的循环,以及我如何能加上所有参加考试的学生的平均值?在


Tags: ofthenumberifcounttotalgradescore
3条回答

对于delim,确保使用while循环进行设置。在

例如:

While X != -1.....

如果你可以使用某个exit的值-1。但通常这是结束while循环的delim,否则您可以选择继续添加值。另一方面,从总金额中取平均值,除以所有累计得分。在

例如: 一个学生参加了一次考试,考得很好,而另一次他没有参加。100 + 0(totalScores) = 100 / 2(exams taken) = .5或者称为50%。为所参加的考试设置一个计数变量,并为所有考试的加分设置另一个变量。这可以帮助你找到平均值。祝你好运!在

似乎您希望通过缩进将以下块放入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): "))

下面是对代码的快速修改。。。像ansh0l一样,我也清理了一下。我没有为你做功课,优化任何东西,也没有努力改进你的错误捕捉代码(也就是说,如果你不输入数字会发生什么?---错误!)。但是,试试看。这似乎对我有用,我能让你的print起作用。在

当心!!!我使用python2.7,而您使用的是python3.*。您可能需要删除我在顶部的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)

相关问题 更多 >