繁体   English   中英

如何从输出中删除括号和逗号(并告诉我我做错了什么)?

[英]How to remove parenthesis and comma from output (and tell me what I did wrong)?

所以我有一个关于 python 类的作业。 我所做的几乎与所需的输出相同。 但是我的打印带有括号和逗号并在一个元组中(我什至不知道它在一个元组中如何)。 我无处指定它是元组。 我做错了什么?

class buttons:
    def __init__(self,word,spaces,border):
        self.word = word
        self.spaces = spaces
        self.border = border

word = "CANCEL"
spaces = 10
border = 'x'
b1 = buttons(word, spaces, border)
print('CANCEL Button Specification:')
print('Button Name:', b1.word)
b1_bord = 1+b1.spaces+len(b1.word)+b1.spaces+1
print('Number of the border characters for the top and the bottom:', b1_bord)
print('Number of spaces between the left side border and the first character of the button \nname:', b1.spaces)
print('Number of spaces between the right side border and the last character of the button \nname:', b1.spaces)
print('Characters representing the borders:', b1.border)
emp = ''
print(b1.border*b1_bord)
print(f'{b1.border,(emp*spaces),b1.word,(emp*b1.spaces),b1.border}')
print(b1.border*b1_bord)
print("=======================================================")
b2 = buttons("Notify",3, '!')
print('NOTIFY Button Specification:')
print('Button Name:', b2.word)
b2_bord = 1+b2.spaces+len(b2.word)+b2.spaces+1
print('Number of the border characters for the top and the bottom:', b2_bord)
print('Number of spaces between the left side border and the first character of the button \nname:', b2.spaces)
print('Number of spaces between the right side border and the last character of the button \nname:', b2.spaces)
print('Characters representing the borders:', b2.border)
print(b2.border*b2_bord)
print(f'{b2.border,(emp*b2.spaces),b2.word,(emp*b2.spaces),b2.border}')
print(b2.border*b2_bord)
print("=======================================================")
b3 = buttons('SAVE PROGRESS', 5, '$')
print('SAVE PROGRESS Button Specification:')
print('Button Name:', b3.word)
b3_bord = 1+b3.spaces+len(b3.word)+b3.spaces+1
print('Number of the border characters for the top and the bottom:', b3_bord)
print('Number of spaces between the left side border and the first character of the button \nname:', b3.spaces)
print('Number of spaces between the right side border and the last character of the button \nname:', b3.spaces)
print('Characters representing the borders:', b3.border)
print(b3.border*b3_bord)
print(f'{b3.border,(emp*b3.spaces),b3.word,(emp*b3.spaces),b3.border}')
print(b3.border*b3_bord)

我得到的输出:

xxxxxxxxxxxxxxxxxxxxxxxxxxxx
('x', '', 'CANCEL', '', 'x')
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
!!!!!!!!!!!!!!
('!', '', 'Notify', '', '!')
!!!!!!!!!!!!!!
$$$$$$$$$$$$$$$$$$$$$$$$$
('$', '', 'SAVE PROGRESS', '', '$')
$$$$$$$$$$$$$$$$$$$$$$$$$

我需要的输出:

xxxxxxxxxxxxxxxxxxxxxxxxxxxx
x         CANCEL           x
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
!!!!!!!!!!!!!!
!   Notify   !
!!!!!!!!!!!!!!
$$$$$$$$$$$$$$$$$$$$$$$$$
$     SAVE PROGRESS     $
$$$$$$$$$$$$$$$$$$$$$$$$$

你可以这样做

def make_btn(word, space, border):
    middle = border + word.center(space*2+len(word)) + border 
    edge = border*len(middle)
    return f'{edge}\n{middle}\n{edge}'

print(make_btn('CANCEL',10,'x'))
print(make_btn('Notify',3,'!'))
print(make_btn('SAVE PROGRESS',5,'$'))

首先,

emp = ''

应该:

emp = ' '


其次,

print(f'{b1.border(emp*b1.spaces),b1.word(emp*b1.spaces),b1.border}')

应该:

print(b1.border+(emp*spaces)+b1.word+(emp*b1.spaces)+b1.border)

您正在使用带有printf 格式化字符串文字,这在这种情况下是不必要的。 只需使用print 而且,使用 '+' 而不是 ',' 来连接字符串。


b2b3重复print语句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM