首页 > 解决方案 > 伪代码中的“真实”表示什么?

问题描述

我正在尝试翻译此伪代码,但无法准确完成。特别是,我似乎无法弄清楚real这里的意思。这是伪代码:

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)

这就是我想出的:

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

标签: pythonpseudocode

解决方案


某些语言使用术语“真实”代替“浮点”等。因此,在 Python 中,使用这段代码可以将其省略。..但除此之外,您的代码还有一些问题。例如你只想

scores=[90,80, 70, 100, 60, 80]

然后只给出平均“分数”和 6

像这样

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

虽然显然没有必要以这种方式这样做,但我认为您只是在学习 Python ......


推荐阅读