Python學習歷程@2011-10-03
執行環境: python 3
這個是個有趣的部分,因為沒有標準答案,所以中間對list使用了sort()
所以最小值就是 inputs[0],當然最大值是list最後一個值,也就是 inputs[-1:][0]
與書上寫的25行剛剛好
print("Type integers, each followed by Enter; or just Enter to finish")total = 0count = 0inputs = []while True: line = input("integer:") if line: try: number = int(line) inputs += [number] except ValueError as err: print(err) continue total += number count += 1 inputs.sort() else: breakif count: print("nmbers:", inputs) print("count=", count, "total=", total, "lowest=", inputs[0], "highest=", inputs[-1:][0], "mean=", total/count)