2

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

1
  • can you add your expected output, also if you have a grade in a test < 60, thx! Commented Apr 4, 2020 at 15:22

6 Answers 6

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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)

Comments

1

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

Comments

0

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]

6 Comments

no, as it doesn't do anything with the fact that you should return only part of it if student fails
@Eric yes, I hope now is better
i don't think the exam counts for 70% if it is the only grade :) unsure though as not specified in question.
@Eric ths OP says If the student fails the test then only the test score is calculated.
exactly, so it counts as the full grade (i think at least). This is also what is done in the original code, albeit in an incorrect loop
|
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()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.