Python程序打印数字模式
程序必须接受整数N作为输入。程序必须打印所需的模式,如示例输入/输出中所示。
例子:
Input : 41325
Output :
|****
|*
|***
|**
|*****
Explanation: for a given integer print the number of *’s that are equivalent to each digit in the integer. Here the first digit is 4 so print four *sin the first line. The second digit is 1 so print one *. So on and the last i.e., the fifth digit is 5 hence print five *s in the fifth line.
Input : 60710
Output :
|******
|
|*******
|*
|
方法
读取输入
对于整数中的每个数字,打印相应的 *s 数
如果数字为 0,则不打印 *s 并跳到下一行
# function to print the pattern
def pattern(n):
# traverse through the elements
# in n assuming it as a string
for i in n:
# print | for every line
print("|", end = "")
# print i number of * s in
# each line
print("*" * int(i))
# get the input as string
n = "41325"
pattern(n)
输出:
|****
|*
|***
|**
|*****
以整数作为输入的替代解决方案:
n = 41325
x = []
while n>0:
x.append(n%10)
n //= 10
for i in range(len(x)-1,-1,-1):
print('|'+x[i]*'*')
# code contributed by Baivab Dash
输出:
|****
|*
|***
|**
|*****