Implement get_average_score function using Python variable *

Asked 1 years ago, Updated 1 years ago, 340 views

The function get_avergae_score() is a function that returns the number of subjects entered and the average test score after receiving a test score for a subject using a variable * and I wonder what code should be put between two annotations to implement this function.

def get_avergae_score(*scores):

    ### ### Modify code here ###  

    ### ### End of your code ###

    return cnt, average

n, avg = get_avergae_score(95, 100, 90)
print(n, "Average score for subjects:", avg, "\n")

n, avg = get_avergae_score(90, 90, 80, 90)
print(n, "Average score for subjects:", avg, "\n")

3. Subject average score: 95.0

4. Subject average score: 87.5

python

2023-04-06 17:10

1 Answers

In Python function definitions, prepending a parameter with * or ** becomes a variable factor list.

See below for a list of variable factors and how to write them:

It's a question of how to add up a list of variable factors, or data of unknown length (similar to an array) through a repetition statement, and at the same time asking for understanding of variable assignments and function returns.

In conclusion, you can write a code that assigns the number of repetitions to cnt and the average of the sum values to average after repeatedly adding the variable factors taken over by *scores by that length.


2023-04-07 09:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.