python |添加图案1 22 333 4444 55555​ ....?

发布于 2025-01-20 13:55:40 字数 316 浏览 1 评论 0原文

我如何添加 1 22 333 4444 55555 ....?

i) 情况 1:n = 1,v = 1

ii) 情况 2:n= 2,v = 23 (注:23 是由 1 + 22 推导出来的)

def create_sequence():

   n = int(input('Enter the number till which you want the sequence:'))

   for i in range(1,n+1):

       print(str(i) * i)

   

create_sequence()

How do i do addition of 1 22 333 4444 55555​ ....?

i) case 1: n = 1, v = 1

ii) case 2: n= 2, v = 23 (Note: 23 is derived as 1 + 22)

def create_sequence():

   n = int(input('Enter the number till which you want the sequence:'))

   for i in range(1,n+1):

       print(str(i) * i)

   

create_sequence()

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

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

发布评论

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

评论(2

淡水深流 2025-01-27 13:55:40

您会生成形式str(i)*i的一系列元素,然后将其投入整数,然后sum该序列。

def create_sequence(n):
    return sum(int(str(i)*i) for i in range(1, n+1))

create_sequence(int(input('Enter the number till which you want the sequence: ')))

You generate a sequence of elements of the form str(i)*i, which you cast into an integer, then sum that sequence.

def create_sequence(n):
    return sum(int(str(i)*i) for i in range(1, n+1))

create_sequence(int(input('Enter the number till which you want the sequence: ')))
别再吹冷风 2025-01-27 13:55:40

利用循环,它们是你的朋友

def create_sequence():

   n = int(input('Enter the number till which you want the sequence:'))
   sum = 0
   for x in range(1, n+1):
       string = ""
       for i in range(1, x+1):
           string += str(x)
       sum += int(string)
   return sum
print(create_sequence())

Take advantage of loops, they are your friend

def create_sequence():

   n = int(input('Enter the number till which you want the sequence:'))
   sum = 0
   for x in range(1, n+1):
       string = ""
       for i in range(1, x+1):
           string += str(x)
       sum += int(string)
   return sum
print(create_sequence())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文