0
numbers = []

while(True):
    num = input("Enter a number or press Enter to stop :")
    if num:
        numbers.append(int(num))
    elif(num == ''):
            break
sum_num =0
for num in numbers:
    sum_num += num
avg = sum_num / len(numbers)
print("The sum is",sum_num)
print("The average is", avg)

The average is in decimal format, however, the sum is not.

The sum is 6
The average is 2.0
Yun
  • 2,536
  • 6
  • 7
  • 26
JSEB
  • 31
  • 5
  • Welcome to SO! Have you tried `print(float(sum_num))`? `/` converts `avg` to decimal but if you're summing ints, you're guaranteed an int. Why would you want that to be a decimal? – ggorlen Sep 13 '21 at 02:41
  • Does this answer your question? [How do I parse a string to a float or int?](https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int) – PCM Sep 13 '21 at 03:04

2 Answers2

1

Just make sum_num a float in the beginning already, use:

sum_num = 0.0

Then appending integers to a float will still retain the float format.

Example:

>>> sum_num = 0.0
>>> sum_num += 1
>>> sum_num
1.0
>>> 

Or you could use:

print("The sum is", float(sum_num))
PCM
  • 2,692
  • 2
  • 7
  • 29
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
0

On your code, int(num) returns interger value. Therefore, change the code

    if num:
    numbers.append(int(num))

to

if num:
    numbers.append(float(num))