Default arguments


#1

Have an assignment due at midnight… here is the question and I will post my code.

Students in a course need to do lab assignments and take tests. Course grade is calculated from these scores. Write a program to calculate course grade. You must write and use the following two functions.

(a) A main function: First, ask the user how many labs there are. Use a loop to enter lab scores and store them in a list. Display the list of lab scores. Second, ask the user how many tests there are. Use a loop to enter test scores and store them in another list. Display the list of test scores. Third, tell the user that the default weights for labs and tests are 50 and 50. If the user wants to use the default weights, enter D. Otherwise, enter C. If the user chooses to use default weights, call the grade_calculator function and pass the list of lab scores and the list of test scores as two arguments. Do not pass any arguments about weights. If the user chooses not to use default weights, ask the user to enter the weights for labs and tests, respectively. Then call the grade_calculator function and pass the list of lab scores, the list of test scores, lab weight and test weight as four arguments. You are free to use positional or keyword arguments.

(b) A grade_calculator function: This function has four parameters: lab score list, test score list, lab weight and test weight. Use default parameter for lab weight and test weight. Default values are 50 and 50. First, calculate and display average lab score. Second, calculate and display average test score. Third, use average lab score, average test score, lab weight and test weight to calculate course grade. Display course grade.

(I have the top, I think, but passing the arguments after the choice is made is stumping me been working on this for two days! Please help I keep getting a traceback error )

code below:

def main():
    labs = int(input('Enter the number of labs: '))
    lab_score = []
    i = 0
    while i < labs:
        new_lab = int(input('Enter a lab score: '))
        lab_score.append(new_lab)
        i = i + 1
    print('Lab scores: ', lab_score)

    tests = int(input('Enter the number of tests: '))
    test_score = []
    i = 0
    while i < tests:
        new_test = int(input('Enter a test score: '))
        test_score.append(new_test)
        i = i + 1
    print('Test scores: ', test_score)
    print('The default weights for course grade are 50% labs and 50% tests.')
    weight = input('Enter C to change the weights or D to use default weights: ')
    if weight == 'C':
        weight1 = float(input('Enter percentage for labs (without the percent sign): '))
        weight2 = float(input('Enter percentage for tests (without the percent sign): '))
        grade_calculator(l=lab_score, t=test_score, w1=weight1, w2=weight2)

    def grade_calculator(l, t, w1, w2):
        lab_avg = sum(l) / len(l)
        print('Lab average: ', lab_avg)
        test_avg = sum(t) / len(t)
        print('Test average: ', test_avg)
        course_grade = (lab_avg * w1) + (test_avg * w2)
        print('Course grade: ', course_grade)
    else:
        grade_calculator(lab_score, test_score)

    def grade_calculator(l, t):
        lab_avg = sum(l) / len(l)
        print('Lab average: ', lab_avg)
        test_avg = sum(t) / len(t)
        print('Test average: ', test_avg)
        course_grade = (lab_avg * .5) + (test_avg * .5)
        print('Course grade: ', course_grade)
main()

#2

here:

    else:
        grade_calculator(lab_score, test_score)

else always have to be after if:

if condition:
    // do something
else:

or elif:

if condition: 
   // do something
elif another condition:
   // do something different
else:

you have neither if or elif, so you can’t use else there


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.