Reputation: 37
I am trying to stop this loop when user enters a number <=0 or when user only cklicks enter. how do i create the break when user only cklick enter?
number = int(input('Type a number, [to stop type 0 or less]'))
num_sum = number
times = 0
while True:
number = int(input('Type a number, [to stop type 0 or less]'))
num_sum += number
times += 1
if number <= 0:
break
average = num_sum / times
print(f'{times} number received \nThe average of number recceived is:'+'{:.4f}'.format(average))
Upvotes: 2
Views: 44
Reputation: 33351
Call input()
and int()
as two separate steps.
while True:
# call input() by itself, without calling int()
answer = input('Type a number, [to stop type 0 or less]')
# if the user pressed enter without typing an answer, break
if not answer:
break
# otherwise convert answer to an integer
number = int(answer)
# remaining code is unchanged
Upvotes: 2