所以我有一個關于 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 $
$$$$$$$$$$$$$$$$$$$$$$$$$
uj5u.com熱心網友回復:
你可以這樣做
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,'$'))
uj5u.com熱心網友回復:
首先,
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對b2和
重復該陳述句b3。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/353545.html
上一篇:基于html汽車商城網站頁面設計與實作.rar共計頁面30+(畢業設計+畢業原始碼+答辯PPT)
下一篇:檢查整數是否只包含奇數
