用Python制作符合这些规范的空心盒?
我要“编写一个python程序,提示用户输入一个正整数n。然后该程序打印一个包含n行和2*n列的空心矩形。例如,输入3将输出:” 我的代码是:用Python制作符合这些规范的空心盒?,python,Python,我要“编写一个python程序,提示用户输入一个正整数n。然后该程序打印一个包含n行和2*n列的空心矩形。例如,输入3将输出:” 我的代码是: n=int(input('Please enter a positive integer between 1 and 15: ')) for col in range(n): for row in range(n): print('*' if col in(0,(2*n)+1) or row in(0,n+1) else ' ',
n=int(input('Please enter a positive integer between 1 and 15: '))
for col in range(n):
for row in range(n):
print('*' if col in(0,(2*n)+1) or row in(0,n+1) else ' ', end=' ')
print()
但是我的输出看起来一点也不像我需要的;它就像一个空心盒子的上半部分和左半部分。此外,我没有得到我需要的双栏。我做错了什么
编辑:谢谢大家的帮助!给了我很多见解,非常有帮助。我将代码修改为以下内容,效果非常好:
>n=int(input('Please enter a positive integer between 1 and 15: '))
>for row in range(n):
> for col in range(2*n):
> print('*' if row in(0,n-1) or col in(0,(2*n)-1) else ' ', end=' ')
> print()
向用户2357112发出特殊呼喊;你让我清楚地意识到我犯了什么错误 代码应该如下所示:
line = "*"*(2*n)
print line
s = "*" + " "*(n-2) + "*"
for i in range(n-2):
print s
print line
该行:
"*"*(2*n)
指定字符串“*”,重复2*n次。你想要2*n列的权利。。这将打印2*n“*”代码:
def printStars(length):
l = ['*'*length]
l+= ['*' + ' '*(length-2) + '*'] * (length-2)
l+= ['*'*length]
return l
if __name__=='__main__':
print('\n'.join(printStars(15)))
输出:
***************
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
***************
希望这有帮助:)
编辑:修正了一些格式
def make_box():
size = int(input('Please enter a positive integer between 1 and 15: '))
for i in range(size):
if i == 0 or i == size - 1:
print("*"*(size+2))
else:
print("*" + " "*size + "*")
make_box()
输出:(n=15)
我的解决方案:
# Response to StackOverflow post:
# Making a hollow box in Python
# The multiplication operator (*) is the same as repeated
# concatenation when applied to strings in Python.
# I solved the problem by creating an array with N elements
# and gluing them together (str.join(array)) with newline
# characters.
# I trust you're already familiar with string formatting and
# concatenation, but in case you're not, please feel free to
# ask for clarification.
def main():
n = int (input("Enter an integer between 1 and 15"))
box = "\n".join(["*"*(2*n)] + ["*%s*" % (" "*(2*n-2))]*(n-2) + ["*"*(int(n>1)*2*n)])
print (box)
if __name__ == '__main__':
main()
input() # Prevents the console from closing immediately
至于你的解决办法。在我看来,循环条件是一团糟;列循环和行循环的顺序相反,列循环中range()的参数应为2*n(因为这是与每行相交的列数)。您还应该再次查看第一条打印语句中的条件。我的解决方案更为基本。请考虑:
row = abs(eval(input('Enter row: ')))
col = abs(eval(input('Enter column: ')))
print ('*'*col)
for i in range (row-2):
print ('*'+' '*(col-2)+'*')
print ('*'*col)
请将您的问题正确格式化。:)否则人们会开始投你的票。谢谢!我注意到它的格式不好,并试图自己修复它。当我做对的时候,你已经给我弄到了:)你的循环倒过来了;在转到下一行之前,必须完成整行的打印,而不是整列。此外,您只在
n
列上循环,边界检查错误。最后一行是行n-1
,而不是n+1
,最后一列是2*n-1
,而不是2*n+1
。不,这不随长度值而变化。行数应等于长度。你没有做到。对不起,Aswin,我已经相应地编辑了代码。谢谢你的介绍。:)当我试着运行这段代码时,我得到了一个语法错误。另外,我不明白星号在这里的作用是什么,在被引用的星号和(2*n)之间?我是否误解了你的问题,把它变成了一个更大的空框:pHello,在阅读之后。请给出你的答案,并详细说明它为什么能解决这个问题,作为建议。谢谢
# Response to StackOverflow post:
# Making a hollow box in Python
# The multiplication operator (*) is the same as repeated
# concatenation when applied to strings in Python.
# I solved the problem by creating an array with N elements
# and gluing them together (str.join(array)) with newline
# characters.
# I trust you're already familiar with string formatting and
# concatenation, but in case you're not, please feel free to
# ask for clarification.
def main():
n = int (input("Enter an integer between 1 and 15"))
box = "\n".join(["*"*(2*n)] + ["*%s*" % (" "*(2*n-2))]*(n-2) + ["*"*(int(n>1)*2*n)])
print (box)
if __name__ == '__main__':
main()
input() # Prevents the console from closing immediately
row = abs(eval(input('Enter row: ')))
col = abs(eval(input('Enter column: ')))
print ('*'*col)
for i in range (row-2):
print ('*'+' '*(col-2)+'*')
print ('*'*col)