如何在python中存储while循环和哨兵的结果? [英] How to store results from while loop and sentinel in python?

查看:52
本文介绍了如何在python中存储while循环和哨兵的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经为此工作了几个小时,以为我已经解决了,但事实证明我完全错了.

been working on this for hours, thought i had it down but it turns out i have it all wrong.

任务是

编写一个程序来计算你的学期平均成绩和课程成绩

*******用户将输入这些数字:******

*******The user will enter these numbers:******

  • 测验分数的列表.每个分数都在 0-10 的范围内.用户输入标记值–1 以结束输入.放弃最低测验分数.*

  • A list of quiz scores. Each score is in the range 0–10. The user enters the sentinel value –1 to end the input. Drop the lowest quiz score.*

项目分数的列表.每个分数都在 0-10 的范围内.用户输入标记值 –1 以结束输入.不要降低程序的最低分数.*

A list of project scores. Each score is in the range 0–10. The user enters the senti- nel value –1 to end the input. Do not drop the lowest program score.*

两次期中考试成绩.每个分数在 0-100* 范围内*

Two midterm exam scores. Each score is in the range 0–100*

期末考试成绩.每个分数都在 0 到 100 的范围内."

A final exam score. Each score is in the range 0–100."

这是我的代码

    qsum = 0
    psum = 0
    count = 0

while True:
    q1 = float(input("Quiz #1 ----- "))
    if q1 < 0:
        break
    qsum = qsum + q1
    lowest = q1
    q2 = float(input("Quiz #2 ----- "))
    if q2 < 0:
        break
    qsum = qsum + q2
    if lowest > q2:
        lowest = q2
    q3 = float(input("Quiz #3 ----- "))
    if q3 < 0:
        break
    qsum = qsum + q3
    if lowest > q3:
        lowest = q3
    q4 = float(input("Quiz #4 ----- "))
    if q4 < 0:
        break
    qsum = qsum + q4
    if lowest > q4:
        lowest = q4
    q5 = float(input("Quiz #5 ----- "))
    if q5 < 0:
        break

print("Quiz #1 ----- ",q1)
print("Quiz #2 ----- ",q2)
print("Quiz #3 ----- ",q3)
print("Quiz #4 ----- ",q4)
print("Quiz #5 ----- ",q5)

while True:
        p1 = float(input("Program #1 -- "))
        if p1 < 0:
            break
        psum = psum + p1
        p2 = float(input("Program #2 -- "))
        if p2 < 0:
            break
        psum = psum + p2
        p3 = float(input("Program #3 -- "))
        if p3 < 0:
            break
    #and so on#

if 90 <= total <= 100:
    print("Grade ------ A")
if 80 <= total < 90:
    print("Grade ------ B")
if 70 <= total < 80:
    print("Grade ------ C")
if 60 <= total < 70:
    print("Grade ------ D")
if 0 <= total < 60:
    print("Grade ------ F")

这是打印出来的样子

Quiz #1 ----- 10
Quiz #2 ----- 9.5
Quiz #3 ----- 8
Quiz #4 ----- 10
Quiz #5 –---- -1
Program #1 -- 9.5
Program #2 -- 10
Program #3 -- 9
Program #4 -- -1
Exam #1 ----- 85
Exam #2 ----- 92
Final Exam -- 81
Average ----- 89.4
Grade ------- B

不幸的是,我没有考虑到他可能希望在没有 50 个 if 语句的情况下在一个循环中完成所有这些,并且没有指定每个测验,他希望它一直计算到进入哨兵之前.但我无法弄清楚如何做到这一点?我如何每次通过循环存储信息,以便获得所需的输出?

Unfortunately i didnt think about the fact that he probably wants this all in one single loop without fifty if statements and without specifying each quiz, he wants it to count through however long until the sentinel is entered. But i cant figure out how to do that? How do i store the information each time through the loop so i can get the desired output?

所以是的,我有点迷茫,任何方向都非常有帮助,我在挣扎.谢谢你们.

So yeah im a little lost, any direction is very helpful, im struggling. Thanks guys.

推荐答案

您不希望有固定数量的测验或项目.相反,对每种类型的分数使用循环,这样您就可以不断询问,直到他们的用户没有更多分数可以输入.

You don't want to have a fixed number of quizes or projects. Instead, use a loop for each of those types of scores, so you can keep asking until they user doesn't have any more scores to enter.

我不会为你写整件事,但这里有一种处理测验的方法:

I'm not going to write the whole thing for you, but here's one way to handle the quizes:

quiz_scores = []

while True:
    score = int(input("Quiz #{} ----- ".format(len(quiz_scores)+1)))
    if score == -1:
        break
    quiz_scores.append(score)

quiz_total = sum(quiz_scores) - min(quiz_scores) # add up the scores, dropping the smallest

还有其他方法可以做到.例如,您可以跟踪在循环中更新的运行总和,而不是构建分数列表.您还想记录迄今为止看到的最小分数,以便在最后的总和中减去最低分数.

There are other ways you could do it. For instance, instead of building a list of scores, you could keep track of a running sum that you update in the loop. You'd also want to keep track of the smallest score you've seen so far, so that you could subtract the lowest score from the sum at the end.

这篇关于如何在python中存储while循环和哨兵的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆