Home > Blockchain >  a python program that gets a number from a user and sums its characters except for two particular nu
a python program that gets a number from a user and sums its characters except for two particular nu

Time:05-18

for example, the given number is 4211256: I want to sum all the digits without adding 11; the result of the given number addition will be: 19

I wrote the first part of the program code but I couldn't continue it

n=int(input("Enter number: "))

sum=0

for digit in str(n):
    sum=sum int(digit)
    while 11:
    pass
print("Sum of digits",sum)

CodePudding user response:

Try this:

n = input("Enter number: ")
result = sum(map(int, list(n.replace('11', ''))))

You get rid of '11' by replacing '11' with '' (there's no need to convert n to an integer and then back to a string), and then you sum the remaining digits.

CodePudding user response:

Try this option and calculate the sums in parts:

def get_sum_without_11(s):
    s_without11 = s.split("11")
    return sum([getSum(n) for n in s_without11])

def get_sum(n):
    sum = 0
    for digit in str(n): 
      sum  = int(digit)      
    return sum
    
n=int(input("Enter number: "))

print(get_sum_without_11(s))    
  • Related