Python:如何在“文件”中打印消息;框架;,使用if、for和while控制语句?真糊涂
用户输入消息,他希望消息重复的次数, 以及框架的“厚度”。它在由“*”、“|”和“-”符号组成的框架内打印消息。我的节目:Python:如何在“文件”中打印消息;框架;,使用if、for和while控制语句?真糊涂,python,loops,Python,Loops,用户输入消息,他希望消息重复的次数, 以及框架的“厚度”。它在由“*”、“|”和“-”符号组成的框架内打印消息。我的节目: x = input("Enter the message:\n") y = eval(input("Enter the message repeat count:\n"))`enter code here` z = eval(input("Enter the frame thickness:\n")) for top in range (z): print("|
x = input("Enter the message:\n")
y = eval(input("Enter the message repeat count:\n"))`enter code here`
z = eval(input("Enter the frame thickness:\n"))
for top in range (z):
print("|"*(z-1)+"+"+"-"*(len(x)+2)+"+"+"|"*(z-1))
for repeat in range(y):
print("|"*z,x,"|"*z)
for bottom in range(z):
print("|"*(z-1)+"+"+"-"*(len(x)+2)+"+"+"|"*(z-1))
重复计数3、帧厚度2和消息“Hello World”的输出示例
但这不是必要的框架。这就是它的外观:
+---------------+
|+-------------+|
|| Hello World ||
|| Hello World ||
|| Hello World ||
|+-------------+|
+---------------+
我使用3个不同的循环正确吗?我正在努力“双倍”循环帧的顶部和底部,以便在每个帧级别中,每个角都有一个“+”,沿帧的顶部和底部有一个“-”,沿左侧和右侧有一个“|”。我认为这将是一个类似的概念,打印出一个金字塔在底部和一个倒金字塔在顶部,但我没有得到任何地方。我在上面做的程序看起来最接近预期的结果。正如你所看到的,顶部和底部的每个“框架”都是不同的;(
以下是实际说明:
编写一个程序,在连续几行重复的消息周围画一个框架(由+-|字符组成)。消息前后都有空格,同心框之间没有空格
请提供帮助。Python V3.x您可以使用循环变量作为深度提示:
x = " %s "%x # Add the spaces
for depth in range(z):
# depth will be 0 on the first iteration, then 1
print("|"*depth+"+"+"-"*(len(x)+2*(z-depth-1))+"+"+"|"*depth)
for repeat in range(y):
print("|"*z+x+"|"*z)
for depth in reversed(range(z)):
# reverse the depth values to start from the inner frame
print("|"*depth+"+"+"-"*(len(x)+2*(z-depth-1))+"+"+"|"*depth)
显然有不止一种方法,下面是一个迭代示例:
您可以使用一个函数,该函数将消息作为输入,并以1级帧输出相同的消息,然后根据需要多次应用该消息
def _one_frame(text): # text is supposed to be a list of lines
lt = len(text[0])
horz = '+' + '-'*lt + '+' # Make the horizontal line +-------+
result = [horz] # Top of the frame
for line in text:
result.append( '|'+line+'|' ) # Add the borders for each line
result.append(horz) # Bottom of the frame
return result
def frame(text, repeat, thickness):
text = [" %s "%text]*repeat # add spaces and repeat as a list
for i in range(thickness):
text = _one_frame(text) # draw one frame per iteration
return '\n'.join(text) # join lines
print(frame('Hello World', 3, 2))
首先,您需要将
z
替换为top
。然后,还有更多的逻辑,请考虑一下:
for top in range(z):
print '|'*top + "+" + "-"*(x+2 + (z-1)*2 - top*2) + "+" + "|"*top
首先,合理地命名变量,这样更容易理解和更正:
message = input("Enter the message:\n")
repeats = eval(input("Enter the message repeat count:\n"))
thickness = eval(input("Enter the frame thickness:\n"))
然后,您需要使框架顶部执行以下操作:
widthWithFrame = len(message) + thickness*2
for i in range(0, thickness) :
print("|"*i + "+" + "-"*(widthWithFrame-(i+1)*2) + "+" + "|"*i)
剩下的我想你可以从那里弄清楚。注意,你的示例输出包括代码中没有包含的“Hello World”周围的额外间距,因此如果你想考虑到这一点,你可能需要从“-(I+1)*2”中删除“+1”。一个使用
\uu str\uu
方法表示字符串的面向对象解决方案
class MultiMessage(object):
"""represents the repeated message"""
def __init__(self, text, count):
self.text = text
#self.text = " %s " % text #optionally add some spaces
self.count = count
def message(self):
"""data representation is a simple list"""
return [self.text] * self.count
def __str__(self):
return "\n".join(self.message())
class Frame(object):
"""a message wrapped in any number of frames"""
def __init__(self, msg, thickness):
self.msg = msg
self.thickness = thickness
@staticmethod
def wrap(message):
"""wrap an array of data with a single frame and return as new data"""
rows = len(message)
cols = len(message[0])
top = ''.join(['+'] + ['-' * cols] + ['+'])
return [top] + ["|%s|" % row for row in message] + [top]
def __str__(self):
"""wrap message and output a string"""
result = self.msg.message()
for i in xrange(self.thickness):
result = self.wrap(result)
return "\n".join(result)
现在可以像在您的示例中一样轻松地使用它
message = raw_input("Enter the message:\n")
count = int(raw_input("Enter the message repeat count:\n"))
thickness = int(raw_input("Enter the frame thickness:\n"))
m = MultiMessage(message, count)
f = Frame(m, thickness)
print f
“%s”到底是做什么的?在第一个解决方案中,它将“%s”打印为消息。第二个解决方案可以工作,但我并不真正理解它,因为我们没有详细学习如何使用“append”和“return”。我非常感谢您的努力。非常感谢。Oops.%s用于字符串插值(我可能应该改为使用str.format)“%s“%”“hello”用hello替换了%s,我只是忘记了替换操作,修复了一些合理的名称,但混淆了
message = raw_input("Enter the message:\n")
count = int(raw_input("Enter the message repeat count:\n"))
thickness = int(raw_input("Enter the frame thickness:\n"))
m = MultiMessage(message, count)
f = Frame(m, thickness)
print f