python - 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?
解决方案
推荐阅读
- swift - 为什么 SCNNode 的 worldPosition 属性与它的实际位置不匹配?
- python - 从 Python 中的相邻文件夹导入模块
- android - 如何在多行edittext中禁用滚动
- python - 如何在python中检测蓝牙设备何时连接
- tensorflow - Tensorflow:经过一系列卷积和反卷积后得到相同的张量
- python - 将 Python 请求库转换为 Nodejs
- r - R嵌套for循环结构,用于两个变量的所有组合
- swift - Swift 5. 'withUnsafeBytes' 已弃用:使用 `withUnsafeBytes
(...) 反而 - java - GeoFire Firebase 查询无法在 ArrayList 中存储用户 ID
- scala - Upickle:读取可能是 String 或 Int 的属性作为 String