Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我的函数/算法输出两次?_Python_Algorithm - Fatal编程技术网

Python 为什么我的函数/算法输出两次?

Python 为什么我的函数/算法输出两次?,python,algorithm,Python,Algorithm,我试着做了一个基本的算法,但是它可以工作,总和和平均数打印两次,小的和最大的数字有时打印两次或不打印 revenues_list = [] answer = input("Do you have data to record? ") while (answer != "no"): try: earnings = float(input("How much was earned? ")) except: prin

我试着做了一个基本的算法,但是它可以工作,总和和平均数打印两次,小的和最大的数字有时打印两次或不打印

revenues_list = []
answer = input("Do you have data to record? ")

while (answer != "no"):
  try:
    earnings = float(input("How much was earned? "))
  except:
    print("You need to give a number with digits.")
  answer = input("Do you have more data to record? ")
  revenues_list.append(earnings)

print(revenues_list)

#make a function that finds the maximun value of the list
def getMaximun(L):
   myMaximun= L[0]
   for element in revenues_list:
    if(myMaximun > element):
      myMaximun = element
      print("Your largest element is: " + str(myMaximun))
#make a function that finds the minimun value of the list
def getMinimun(L):
  myMinimun= L[0]
  for element in revenues_list:
    if(myMinimun < element):
      myMinimun = element
      print("Your smallest element is: " + str(myMinimun))
#make a function that finds the average of the list
def getAverage(L):
  sum = 0
  for element in revenues_list: 
    sum = sum + element
    average = sum/len(L)
    print("Your sum revenue is: " + str(sum) + "\n" + "Your average revenue is: " + str(average))
getMaximun(revenues_list)
getMinimun(revenues_list)
getAverage(revenues_list)
收入清单=[] 回答=输入(“您有数据要记录吗?”) 而(回答!=“否”): 尝试: 收益=浮动(输入(“挣了多少?”) 除: 打印(“您需要给出一个带数字的数字。”) 回答=输入(“您有更多数据要记录吗?”) 收入列表。附加(收益) 打印(收入清单) #生成一个函数,用于查找列表的最大值 def getMaximun(L): myMaximun=L[0] 对于收入列表中的元素: 如果(myMaximun>元素): myMaximun=元素 打印(“您的最大元素是:“+str(myMaximun)) #生成一个函数,用于查找列表的最小值 def getMinimun(L): myMinimun=L[0] 对于收入列表中的元素: 如果(myMinimun<元素): myMinimun=元素 打印(“您的最小元素是:“+str(myMinimun)) #创建一个函数,用于查找列表的平均值 def getAverage(升): 总和=0 对于收入列表中的元素: 总和=总和+元素 平均值=总和/长度(L) 打印(“您的总收入为:“+str(sum)+”\n“+”您的平均收入为:“+str(average)) getMaximun(收入列表) getMinimun(收入列表) getAverage(收入列表)
我将选择其中一个例子。如@Damien评论中所述,您必须将打印语句移出
for
循环,例如:

for element in revenues_list:
    if(element > myMaximun):
        myMaximun = element
print("Your largest element is: " + str(myMaximun))
旁注:
对于提出这样一个问题,最好做一个简单的工作示例。读这个。在实际需要StackOverflow之前,使用这种方法通常也有助于调试/解决问题。

注意,
print
处于循环中……旁注:一般来说,除调试目的外,计算某些内容的函数不应打印任何内容。我建议您使用
return
并将值返回给调用者,从而切换编写这些函数的方式(至少在开始编码时是如此)。因此,在每个
get…
函数的末尾,使用
return myResult
而不是
print(myResult)
(在正确的位置,请参见现有答案),在调用函数的地方,使用
print(getXxx(收益列表))
,或者如果需要,使用中介。该代码片段实际上会计算最小值,不是最大值!那是正确的,我只是从问题中抄来的。我会编辑。