【发布时间】:2022-10-21 21:13:08
【问题描述】:
创建一个程序,它将接受一个正整数和负整数并将其存储在一个列表中,直到用户输入 /。显示所有输入的总和和平均值以及可以找到的最高和最低值和索引号。 Python 编程 这是我正在关注的示例:
Sample input/output:
Enter No. 1: 45
Enter No. 2: 89
Enter No. 3: 35
Enter No. 4: 90
Enter No. 5: 88
Enter No. 6: /
The sum of all inputs is 347
The average of all inputs is 69.40
The highest input is 90 found at the index 3
The lowest input is 35 found at the index 2
底部是我使用while循环的代码。我需要知道是什么问题。 我正在使用python编程语言。
from statistics import mean
ows=[]
ct=1
while True:
num=input("Enter No." + str(ct) + ": ")
ct+=1
if num=="/":
break
num=int(num)
ows.append(num)
av = sum(ows)/len(ows)
print("THe sum of all inputs is", sum(ows))
print("The average of all inputs is", "%.2f"%av)
print("The highest input is",max(ows),f"found at the index")
print("The lowest input is",min(ows),f"found at the index")
【问题讨论】:
-
您缺少 max 和 min 的索引,您可以使用
argmax和argmin来实现 -
那么你可以发送一个例子或图片吗?所以我可以看到我错过了什么?
-
你自己做代码吗?或者你只是从别人那里拿了代码?
-
我的评论给你一个关于如何解决它的提示。但是想要已经为您编码的解决方案让我怀疑您可能没有编写其余代码。
-
(如果问题是“我怎样才能找到最低和最高数字的索引?”,f / e,这比“不工作”更可行)
标签: python while-loop