我正在嘗試構建一個在線MASH遊戲。我如何輸出:Python 2.7如果middle_name爲null打印其他東西打印其他東西
打印「歡迎來到節目{} {}」格式(self.first_name,self.last_name)如果沒有輸入中間名,但:
打印「歡迎來到節目{ } {} {}」格式(self.first_name,self.middle_name,self.last_name)如果輸入中間名:
類名稱(對象):
def __init__(self, first_name='', last_name='', middle_name=''):
if not first_name:
first_name = raw_input("What is your first name: ")
if not last_name:
last_name = raw_input("What is your last name: ")
if not middle_name:
middle_name = raw_input("What is your middle name[Leave blank if not applicable]: ")
if middle_name == '':
print "No middle name here!"
self.first_name = first_name
self.last_name = last_name
self.middle_name = middle_name
self.print_name()
def print_name(self):
if not middle_name:
print "Welcome to the program {} {}".format(self.first_name, self.last_name)
else:
print "Welcome to the program {} {} {}".format(self.first_name,
self.middle_name,
self.last_name)
克里斯=名稱()
到底是什麼問題? – Mureinik
如果未輸入middle_name原始輸入,我希望程序輸出不同的東西 – Chris
如果在'__init__'方法中將'middle_name'參數默認爲'None',('def def_name(first_name,last_name,middle_name = None ):')你的代碼似乎在做你所描述的 – davedwards