n = int(input("Enter a value:
"))
Once you hit an Exception for invalid input for your int()
cast, the "Done" value is never assigned to n
. So, n == "Done"
is effectively using the old value of n
from the previous iteration or None
if it's the first iteration.
Try the following:
quantity = 0
sum = 0
avg = 0
n = None
input_val = None
while input_val != "Done":
try:
input_val = input("Enter a value:
")
n = int(input_val)
sum = sum + n
quantity = quantity + 1
avg = sum / quantity
except:
print("Enter a number!")
print("Process is Done!")
print("Sum : {}, Quantity : {}, Avg : {}".format(sum, quantity,avg))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…