0

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

The assignment is to

Write a program that computes your semester average and letter grade for the course

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

  • 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.*

  • 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.*

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

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

And here is my code

    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")

Here is what the print out needs to look like

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

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.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

This should be able to be completed using only while loops and it's driving me crazy, is there a way to write a loop where the variable changes from "Quiz #1 ------ " to "Quiz #2 ----- " while still storing the values that i put in each time through the loop??
Can you clarify what exactly you mean by "only while loops"? What exactly does my answer contain that it shouldn't? There's no sane way to solve this problem with lots of variables like q1, q2 etc. Any time you find yourself numbering your variables (and getting past 2) you're almost certainly doing it wrong. Use a list instead. If you're not allowed to use lists for some reason then you probably want to be calculating a running sum.
ok I think i get what you're sayin now. I am getting an error when i run this though, it;s saying syntax error at the "if score == -1:" not sure whats happenin there. I think you're totally right about numbering variables and getting past 2 though. it definitely didnt feel right. Any idea about the error?
Ah, I had a missing parenthesis on the previous line. I've updated to fix it.
Ok this makes the most sense so far, is there any way to count how many times a quiz score has been entered? Can i divide the quiz_total by a counter or something to get the average?
|
0

If you're storing the input as integers, you probably want a dict. You'd do something like this:

numberOfInputs = 10
names = ["Quiz 1", "Quiz 2", "Program 1", "Exam 1", "Final Exam"]
d = {}
for name in names:
    i = int(input(name.rjust(4)))
    if i < 0:
        quit()
    d[name] = i
# calculate the ave
for name in d:
    print(name)
print("Average: " + ave)

You'd check if you need to quit every time and you'd also have a fairly intuitive way of accessing the data.

8 Comments

That's the problem though, he wants it to keep going through the loop till the user enters the sentinel "-1". So basically if the user wanted there to be 3 quizzes they would just enter "-1" on the fourth time they were prompted. or they could do that for the 35th time through the loop if there were that many quizzes. Does that make sense?
ah, I see. I think dicts are the way to go then - one moment while I change my answer
Im not sure a dict would work either as "Quiz #1 ----- " "Quiz #2 ------" etc is not inputed by the user, that should be a prompt. is there a way to do that? We havent covered dicts or arrays so it would most likely be in a while loop with a sentinel.
I'm not sure what you mean by a sentinal. In any case, you could add '-------' programmatically, there's no sense typing it out for each name. Also, "We haven't covered <this topic> yet" is one of my pet peeves, there's absolutely no reason that should mean anything.
Sorry i totally understand, i just really want to understand while loops because it will be important for the exam, sentinels are basically things that stop a while loop.
|
0

I used some things that might come in handy in the future, if you continue to program in python (like generators, list comprehension, map)
You can try something like this: (Can be simplified, but for clarity.. )

#!/bin/env python3

"""
Course grade computation
"""

# quiz.py

import sys


def take(name="", dtype='int', sentinel=-1):
    """:param dtype: string representing given input type"""
    counter = 0
    i = 0
    while True:
        i = eval(dtype)(input("{} #{}: ".format(name,counter)))  # We should use function map instead, but for simplicity...
        if i > 10 or i < -1:
            print("Invalid input: input must be in range(0,10) or -1", file=sys.stderr)
            continue
        if i == sentinel:  # <-- -1 will not be added to list
            break
        yield i
        counter += 1


def main():
    grades_map = dict([(100, 'A'), (90, 'B'), (80, 'C'), (70, 'D'), (60, 'F')])

    print("Input quiz scores")
    quizes = [i for i in take("Quiz", dtype='float')]
    quiz_sum = sum(quizes)
    quiz_lowest = min(quizes)

    print("Input project scores")
    projects = [i for i in take("Project", dtype='float')]
    proj_sum = sum(projects)

    print("Input exam scores")
    exam1, exam2, final = map(float, [input("Exam #1:"), input("Exam #2:"), input("Final:")])

    total = ...  # Didn't know what the total really is here
    average = ...  # And same here

    grade = grades_map[round(average, -1)]

    # Handle your prints as you wish


if __name__ == '__main__':
    main()


EDIT: Change in the generator so that -1 is not added to the list

3 Comments

This is super helpful man. Im having trouble calculating the average here. I guess it should be the ((Quiz total-lowest)/however many quizzes *10)+ (program total/however many programs*10) + Exam avg + final. Then that should all be calculated by the weighted averages? im not sure really
This is super helpful man. Im having trouble calculating the average here. I guess it should be the ((Quiz total-lowest)/however many quizzes *10)+ (program total/however many programs*10) + Exam avg + final. Then that should all be calculated by the weighted averages? im not sure really how the counter works here in these.
Glad I could help. Well, about the average, it should be stated in the assignment.. there is just a general formula mean = sum(values)/number_of_values .. I could only quess what that could be here... Maybe ((quiz_mean + proj_mean)*10 + ex_mean + final) / 4? Also, Please, accept the given answer if it helps you achieve the goal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.