首页 > 解决方案 > How to print out the max and min without a definite list?

问题描述

This is the assignment: #Write another program that prompts for a list of numbers as above and at the end prints out both the maximum and minimum of the numbers instead of the average.

def program2():
  sum=0.0
  count=1
  greatest_so_far=None
  smallest_so_far=None

  while True:
    number=input("Please write a number. When you are done, please write 'done.'\n")
    if number=="done":
      break
  try:
    number=float(number)
    sum=sum+number
    count=count+1 
    if number==None or number>greatest_so_far:
      greatest_so_far=number
    elif number==None or number<greatest_so_far:
      smallest_so_far=number
    
  except:
    print("Please use only numbers.")
    #Why doesn't it work when I write "continue" here?
    
  print(sum,count,greatest_so_far,smallest_so_far)

program2()

When I try to run it, the greatest_so_far always comes out as None, as does smallest_so_far. How can I change it so that it displays the largest or smallest number inputted by the user?

标签: pythonloops

解决方案


推荐阅读