here is my code so far. I'm not sure whether I do something wrong on the code because the average seems to be wrong. please help me. Thank you
def enter_score ():
results = []
scores = int(input("How many results to enter? : "))
for i in range(scores):
student_name = input("enter student name: ")
student_score = int(input("Please enter score for student " + student_name + " : " ))
results.append(student_score)
results.append(student_name)
print(results)
return results
def calc_average():
total=0
total=total+student_score
average= total/scores
print("the average is ", average)
return
def above_average():
above_average=0
for i in range (scores):
if results [i] > average:
above_average = above_average + 1
print(" the above average score is ", above_average)
return above_average
enter_score()
calc_average()
above_average()
You're making a list results
that contains scores and names alternating -- very hard to use. You return that list from enter_score
, then completely ignore -- you throw it away! So the other two functions are supposed to work on some magic, or thin air...?
Clearly, the overall flow at the end must instead be:
results = enter_score()
average = calc_average(results)
above_average(results_average)
and calc_average
must end with return average
.
results
is better arranged by replacing the two results.append
calls with a single one:
results.append((student_score, student_name))
ie, make it a list of tuple, not a weird mix of numbers and names.
The other two functions clearly must loop on that list (which they now receive as an argument) to do their respective jobs.
So:
def calc_average(results):
total = 0
for student_score, student_name in results:
total=total+student_score
average= total/float(len(results))
print(average)
return average
and:
def above_average(average, results):
above_average = 0
for student_score, student_name in results:
if student_score > average:
above_average += 1
print(" the number of above average scores is ", above_average)
return above_average
I fixed/ammended your code so that it works:
def enter_score ():
results = []
scores = int(input("How many results to enter? : "))
for i in range(scores):
student_name = input("enter student name: ")
student_score = int(input("Please enter score for student " + student_name + " : " ))
results.append((student_name, student_score))
print(results)
return results
def calc_average(results):
total=0
for student_name, student_score in results:
total=total+student_score
average= total/len(results)
print("the average is ", average)
return average
def above_average(results, average_score):
above_average_no=0
for student_name, student_score in results:
if student_score > average_score:
above_average_no = above_average_no + 1
print(" the above average score is ", above_average_no)
return above_average_no
results = enter_score()
average_score = calc_average(results)
above_average_no = above_average(results, average_score)
I wont provide detailed explanation on what and why things changes. Leave it to you to figure it out. Please note that I tried to make minimal changes to your code. Many things could be improved, such as calculating sum
, etc. Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.