簡體   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