Python Sum of digits with average

Question:

Hello everyone i am a new to programming and i have a small problem in the code, i need to get the average of the sum i already made the sum of digits and here is the code:

num = int(input("Enter a numbern"))
r = 0
m = 0
while num != 0:
    m = num % 10
    r = r + m
    num = int(num / 10)
print(f"The sum of all digits is {result} and the average is")

i need the average but i dont know how to get it( for example in the num i added 5555, 5+5+5+5 = 20) how would i get the average and thanks

i tried to do r / m but also didnt work

Asked By: momoshki

||

Answers:

This is the short solution:

num = input("Enter a numbern")
s = sum([int(x) for x in num])
a = s / len(num)

print(f"The sum of all digits is {s} and the average is {a}")

Also, this is the fixed version of your code:

num = int(input("Enter a numbern"))
r = 0
m = 0
t = 0
while num != 0:
    m = num % 10
    r = r + m
    num = int(num / 10)
    t = t + 1
print(f"The sum of all digits is {r} and the average is {r/t}")
Answered By: Majid
n_str = input("Enter numbers by gaps: ")
n = [float(num) for num in n_str.split()]
summ = 0

for i in n:
    summ = summ + i

print("Average of ", n, " is ", summ / len(n))
Answered By: Karthk P Jiji
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.