python - 伪代码中的“真实”表示什么?
问题描述
我正在尝试翻译此伪代码,但无法准确完成。特别是,我似乎无法弄清楚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))
解决方案
某些语言使用术语“真实”代替“浮点”等。因此,在 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 ......
推荐阅读
- javascript - 模块解析失败:意外令牌 (4:16)
- html - 100%删除宽度后,为什么我的背景图片消失了?
- r - R中形状的每个多边形内的特定点数
- windows - 为什么它说“'flutter'不被识别为内部或外部命令”即使我为flutter设置了正确的PATH变量
- google-apps-script - 如果来自 Google 表单的传入行中的单元格具有特定值,则运行脚本函数
- linux - 以编程方式使用 perf 列表中的 perf 事件
- vhdl - 调试:VHDL 错误案例语句未涵盖所有选择。需要“其他”子句,但定义了所有状态
- python - 这个程序一次又一次地给出答案,我找不到阻止它的方法
- shopify - 突出显示 shopify 中的活动收藏按钮
- c# - 在接口内部的上下文中,“新”运算符是什么意思?