一个Python程序,该程序从用户获得数字并总结其字符,但两个特定号码除外

发布于 2025-01-29 19:54:02 字数 238 浏览 3 评论 0原文

例如,给定的数字为4211256:我想总结所有数字而不添加11;给定编号添加的结果将是:19

我写了程序代码的第一部分,但我无法继续

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

sum=0

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

关于从前 2025-02-05 19:54:02

尝试以下操作:

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

通过用“”替换“ 11”(无需将n转换为整数然后返回到字符串),您可以摆脱“ 11”,然后总结剩余的数字。

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.

聚集的泪 2025-02-05 19:54:02

尝试此选项并计算部分总和:

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))    

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))    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文