Skip to content Skip to sidebar Skip to footer

How To Store Results From While Loop And Sentinel In Python?

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 gr

Solution 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 = []

whileTrue:
    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.

Solution 2:

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 avefor 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.

Solution 3:

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.pyimport sys


deftake(name="", dtype='int', sentinel=-1):
    """:param dtype: string representing given input type"""
    counter = 0
    i = 0whileTrue:
        i = eval(dtype)(input("{} #{}: ".format(name,counter)))  # We should use function map instead, but for simplicity...if i > 10or i < -1:
            print("Invalid input: input must be in range(0,10) or -1", file=sys.stderr)
            continueif i == sentinel:  # <-- -1 will not be added to listbreakyield i
        counter += 1defmain():
    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 wishif __name__ == '__main__':
    main()

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

Post a Comment for "How To Store Results From While Loop And Sentinel In Python?"