【问题标题】:Python White Space Diamond蟒蛇白色空间钻石
【发布时间】:2015-10-19 01:41:01
【问题描述】:

我在这个问题上纠结了几天。整个实验室问题可以找到here.

实验 6:循环实验第 3 部分

为任何正整数 n 打印以下内容。使用输入语句允许用户输入 n 的值,然后打印适当大小的框。

E.g. n = 3

 1 3 5 5 3 1
 3 5     5 3
 5         5
 5         5
 3 5     5 3
 1 3 5 5 3 1

E.g. n = 5

1 3 5 7 9 9 7 5 3 1
3 5 7 9     9 7 5 3
5 7 9         9 7 5
7 9             9 7
9                 9
9                 9
7 9             9 7
5 7 9         9 7 5
3 5 7 9     9 7 5 3
1 3 5 7 9 9 7 5 3 1

不用担心处理多位数字的间距。

这是我目前所拥有的:

from __future__ import print_function

for i in range(5):
    for j in range(5-i):
        print (j, end=" ")

    for k in range(i):
        print (" "*(2**2-1), end=" ")

    for l in range(5-i):
        print (l, end=" ")

    print()

for i in range(5):
    for j in range(i+1):
        print (j, end=" ")

    print()

我使用了从未来函数导入,因为我使用的是 Python 2.7.3.1。另外,如果这似乎是一个“菜鸟”问题,我很抱歉,但我是初学者,需要帮助。谢谢你的帮助!不胜感激。

【问题讨论】:

  • 你还没有问过问题。
  • @AndrewMedico 我清楚地问:“打印以下任何正整数 n。”我还附上了整个实验室的链接。
  • 不,你没有问问题。您粘贴了家庭作业和一些代码。我们不是家庭作业服务。你的代码不起作用吗?如果没有,您期望它没有做什么?

标签: python-2.7 for-loop whitespace


【解决方案1】:

这是一个有趣的问题。这是我的解决方案,不是很优化,但应该很容易理解:

n = 5
import string 
numberList = string.letters
# uncomment if you want numbers, i prefer letters
#numberList = [i*2-1 for i in range(1, n+1)]

#              upper             lower
#                V                 V
for n_row in range(0, n) + list(reversed(range(0, n))):    
    # left number
    for number in numberList[:(n-n_row)]:
        print number,
    # space
    for number in range(n_row):
        print ' ', ' ',
    # right number
    for number in reversed(numberList[:(n-n_row)]):
        print number,
    print 

两位数确实搞砸了格式,所以我改用字母。

a b c d e e d c b a
a b c d     d c b a
a b c         c b a
a b             b a
a                 a
a                 a
a b             b a
a b c         c b a
a b c d     d c b a
a b c d e e d c b a

【讨论】:

  • 这是不正确的,'a' 不应出现在所有行的边缘。
【解决方案2】:

试试下面的代码:

inputValue = int(input('input interger:'))

base = ' '
for i in range(inputValue):
    base = ' '+str(2*(inputValue-1-i)+1)+base+str(2*(inputValue-1-i)+1)+' '

length = len(base.strip())
graph = []
for j in range(inputValue):
    line = ' '
    for k in range(j):
        line = '  '+line+'  '
    for i in range(inputValue-j):
        line = ' '+str(2*(inputValue-1-i)+1)+line+str(2*(inputValue-1-i)+1)+' '
    line = line.strip()
    length_temp = int((length-len(line))/2)
    for m in range(length_temp):
        line = ' '+line+' '
    graph.append(line)
for j in range(inputValue):
    graph.append(graph[inputValue-1-j])
for line in graph:
    print(line)

或者这个代码,取决于你想要的输出是什么:

inputValue = int(input('input interger:'))

base = ' '
for i in range(inputValue):
    base = ' '+str(2*(inputValue-1-i)+1)+base+str(2*(inputValue-1-i)+1)+' '

length = len(base.strip())
graph = []
for j in range(inputValue):
    line = ' '
    for k in range(j):
        line = '  '+line+'  '
    for i in range(inputValue-j):
        line = ' '+str(2*(inputValue-1-i)+1)+line+str(2*(inputValue-1-i)+1)+' '
    line = line.strip()
    length_temp = int((length-len(line)))
    for m in range(length_temp):
        line = line[:int(len(line)/2)]+'  '+line[int(len(line)/2)+1:]
    graph.append(line)
for j in range(inputValue):
    graph.append(graph[inputValue-1-j])
for line in graph:
    print(line)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 2021-09-01
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 2010-10-21
    相关资源
    最近更新 更多