0
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

4 回答 4

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)
于 2013-10-05T02:41:17.747 回答
0

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

前任:

While X != -1.....

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

例如:一名学生参加了一项考试并取得了优异的成绩,而他没有参加另一项考试。100 + 0(totalScores) = 100 / 2(exams taken) = .5否则称为 50%。为参加的考试设置一个计数变量,为所有考试添加另一个分数。这可以帮助您找到平均值。祝你好运!

于 2014-06-25T20:48:54.140 回答
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): "))
于 2013-10-05T02:26:26.170 回答
0

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

#current code
missing = 0
highscore = 0
inclass = 0
takenexam = 0
count = 0
total = 0
average_taken = 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 = float(total) / count
if takenexam > 0 :
    average_taken = float(total) / takenexam

print("\n\n")
print "Grade Result -------------\n"
print "Total number of students in the class: ", count
print "number of students who missed the exam: ", missing
print "number of students in the class: ", inclass
print "number of students who took the exam: ", takenexam
print "number of students who scored high: ", highscore
print("The average grade is %.2f" % average)
print("The average grade for students taking the exam is %.2f" % average_taken)
于 2013-10-05T02:34:31.160 回答