How to store results from while loop and sentinel in python?

后端 未结 3 656
礼貌的吻别
礼貌的吻别 2021-01-26 10:08

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 sem

3条回答
  •  隐瞒了意图╮
    2021-01-26 10:29

    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

提交回复
热议问题