python function calculate grades

Viewed 169

i want to code python program that calculate grades

first list is final exam grade. second list is homework final grade list.

the wight of the score is 70% for exam grade (list 1) and 30% for homework grade

def func_calc(a,b):

    for i in range(len(a)):
        exam1 = a[i]
        homework1= b[i]
         print(0.70*exam1+0.30*homework1)
    e = [70,80,67,89,100]
    h= [100,80,56,89,100]
    func_calc(e,h)

why the list in main() not working in the func_calc(only one arg sent)?

6 Answers

You forgot to indent your if statement in your loop. Try

    for i in range(len(a)):
        exam1 = a[i]
        homework1= b[i]
        if a[i] < 60:
            print (a[i])
        else:
             print(0.70*exam1+0.30*homework1)

Well it doesn't work as your indentation is quite incorrect. You loop over the grades by index assignign them to unused variables. Than you do something with the index outside of the loop (where it is either undefined or takes the last value of the loop so in this case 4). This can be done a lot shorter with a list generator:

def func_calc(a,b):
   return [0.7*exam + 0.3*home if exam >= 60 else exam for exam,home in zip(a,b)]

Your code indentation is incorrect. Please correct the indentation of if-else part as given below:

def func_calc(a,b):
    for i in range(len(a)):
        exam1 = a[i]
        homework1 = b[i]
        if a[i] < 60:
            print (a[i])
        else:
            print (0.70*exam1+0.30*homework1)

I am not sure what is your background but maybe it helps to understand better if there is used java/c# syntax. Python use indentation to show what belongs to nested block. As you have for and if they will be executed sequentially

def func_calc(a,b):

    for i in range(len(a)):
        exam1 = a[i]
        homework1= b[i]
    if a[i] < 60:
        print (a[i])
    else:
         print(0.70*exam1+0.30*homework1)

rewritten to other language will be executed as

void func_calc(a,b){

    for (i = 0; i < a.Length(a); i++){
        exam1 = a[i]
        homework1= b[i]
    }

    if(a[i] < 60){
        print (a[i])
    }
    else{
         print(0.70*exam1+0.30*homework1)
    }
}

So simply indent if so it is executed as part of your for loop

def func_calc(a,b):

    for i in range(len(a)):
        exam1 = a[i]
        homework1= b[i]
        if a[i] < 60:                         # this is indented
            print (a[i])                      # this is indented
        else:                                 # this is indented
             print(0.70*exam1+0.30*homework1) # this is indented

Equivalent in java/c# will be

void func_calc(a,b){

    for (i = 0; i < a.Length(a); i++){
        exam1 = a[i]
        homework1= b[i]

        if(a[i] < 60){
            print (a[i])
        }
        else{
             print(0.70*exam1+0.30*homework1)
        }
    }
}

you are iterating over all your grades and keep only the last one, you can fix this with including your if statement under your for loop


also, you can use a list comprehension :

exam = [70,80,67,89,100]
homework= [100,80,56,89,100]
[0.7*e + 0.3*h if e > 60 else 0.7* e for e, h in zip(exam, homework)]

output:

[79.0, 80.0, 63.7, 89.0, 100.0]

It's your indentation. Should be:

def func_calc(a,b):

    for i in range(len(a)):
        exam1 = a[i]
        homework1= b[i]
        if a[i] < 60:
            print (a[i])
        else:
            print(0.70*exam1+0.30*homework1)

def main():
    exam = [70,80,67,89,100]
    homework= [100,80,56,89,100]
    func_calc(exam,homework)

main()
Related