I'm trying to translate this pseudocode and can't do it accurately. In particular, I can't seem to figure out what real
here means. This is the pseudocode:
Function Real average(Real values[], Integer size)
Declare Real total = 0.0
Declare Integer counter = 0
While counter < size
Set total = total + values[counter]
Set counter = counter + 1
End While
Return total / size
End Function
Declare Real scores[6] = 90, 80, 70, 100, 60, 80
Display average(scores, 6)
And this is what I've come up with:
def average(values[], int(size))
total = 0.0
counter = 0
while counter < size:
total = total + values[counter]
counter = counter + 1
return total / size
scores[6] = 90, 80, 70, 100, 60, 80
print(average(scores, 6))
Some languages use the term "real" in place of "float" etc. Therefore, in Python, with this bit of code you can leave it out. ..but there are a few things wrong with your code other than that. For example you just want
scores=[90,80, 70, 100, 60, 80]
then just give average "scores" and 6
Like this
def average(values ,size):
total = 0.0
counter = 0
while counter < size:
total = total + values[counter]
counter = counter + 1
return total / size
scores = [90, 80, 70, 100, 60, 80]
print(average(scores, 6))
Whilst clearly it is not necessary to do this in this way, I presume you are just learning Python...