【问题标题】:Write a program that gets an integer , which its number of digits is not specified. then print each digit n times if n is the digit itself [closed]编写一个程序,获取一个未指定其位数的整数。如果 n 是数字本身,则打印每个数字 n 次 [关闭]
【发布时间】:2022-01-03 05:51:05
【问题描述】:

我该如何解决这个问题? 像这样: 6041 6:666666 0: 4:4444 1:1 我只知道为了分隔数字,我们应该这样做,我认为:

num = int(input)
while num > 0:
res = num // 10
num = num % 10

【问题讨论】:

  • 很酷,但你的问题是什么?
  • 啊抱歉,这个程序怎么写?你能帮帮我吗?
  • 嗯,你的第一个 sn-p 还不错(调整缩进)。现在只剩下通过复制输出数字(res)了。

标签: python algorithm python-2.7 for-loop while-loop


【解决方案1】:

一种方法是将数字保留为字符串,然后在循环中将第一个数字转换为 int,打印 n 次,然后将其删除。重复循环,直到字符串为空:

num = input("Input a number: ")
while len(num) > 0:
    digit = num[0]
    for i in range(int(digit)):
        print(digit, end="")
    num = num[1:]

【讨论】:

  • 谢谢!!!! ^_^
  • @Mee 如果解决了您的问题,请接受我的回答
  • 是的,当然:)
猜你喜欢
  • 2012-06-17
  • 2022-01-10
  • 2020-05-05
  • 2017-04-24
  • 1970-01-01
  • 2022-11-12
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多