0

I tried to make a basic algorithm and it works however, the sum and average print out twice, and the small and largest digit sometimes prints twice or doesn't print.

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)
2
  • 1
    Note that the print is in the loop ... Commented Dec 2, 2020 at 13:40
  • 2
    Side comment: in general, and except for debug purposes, functions that compute something should not print anything. I advise you to switch your way of writing those functions (that is usual at least when starting coding), by using return and return the value to the caller. So, at the end of each get... function, use return myResult instead of print(myresult) (at the correct location, see existing answer), and where you call the function use print(getXxx(revenues_list)), or use an intermediary if you want. Commented Dec 2, 2020 at 13:55

1 Answer 1

1

I will pick one of the examples. As noted in the comment from @Damien you have to move the print statement out of the for loop, so e.g. like this:

for element in revenues_list:
    if(element > myMaximun):
        myMaximun = element
print("Your largest element is: " + str(myMaximun))

Side Note: For asking such a question it is a good idea to make a minimal working example. Read this stackoverflow page. Using this approach can also often help in debugging/solving the problem before actually needing StackOverflow.

Sign up to request clarification or add additional context in comments.

2 Comments

That code snippet would actually compute the minimum, not the maximum!
That is correct, I just copied it from the question. I will edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.