Python快速编程入门答案(python快速编程入门编程题答案)

http://www.itjxue.com  2023-03-07 16:14  来源:未知  点击次数: 

《python从入门到实践》练习题有答案吗

找不到,只找到找到几道题目的答案

8-9 ?魔术师:创建一个包含魔术师名字的列表,并将其传递给一个名为show_magicians() 的函数,这个函数打印列表中每个魔术师的名字。8-10 ?了不起的魔术师:在你为完成练习 8-9 而编写的程序中,编写一个名为make_great() 的函数,对魔术师列表进行修改,在每个魔术师的名字中都加入字样“theGreat”。调用函数 show_magicians() ,确认魔术师列表确实变了。想问下大神怎么对魔术师列表进行修改同时又不使用新的列表,我修改了一次但是用了一个新列表的方法和原习题不符。

初学者学Python编程如何快速入门?

电子书集合|数据科学速查表|迁移学习实战 ,免费下载

?链接: ? 提取码: z9x7

python 编程 求答案!2、3两题

#!/usr/bin/env?python

#coding=utf-8

import?re

from?datetime?import?datetime?as?dt,?timedelta

import?platform

if?platform.python_version()[:1]?==?'2':?#判断python版本是2还是3

????import?sys

????reload(sys)

????sys.setdefaultencoding('utf8')

class?Idcard(object):

????'''?

?????m?=?Idcard('225122198611134730')

?????print(m.sex)

????男

?????m.birth

????'1986-11-13'

?????m.age

????30

????'''

????def?__init__(self,idcard):

????????self.idcard?=?idcard????????

????????if?len(idcard)?==?15:

????????????sex,?birth?=?idcard[-1:],?'19'?+?idcard[6:12]

????????elif?len(idcard)?==?18:

????????????sex,?birth?=?idcard[-2:-1],?idcard[6:14]???

????????else:

????????????raise?Exception('len(idcard)?is?{}?(15/18)'.format(len(idcard)))

????????self._sex?=?int(sex)?%?2

????????self._birth?=?birth

????

????@property

????def?sex(self):

????????return?u'男'?if?self._sex?%?2?else?u'女'

????@property

????def?age(self):??

????????now,?bir?=?dt.now(),?dt.strptime(self._birth,?'%Y%m%d')

????????beforebirth?=?(now?-?dt(now.year,?bir.month,?bir.day)).days??0

????????return?dt.now().year?-?int(self._birth[:4])?-?beforebirth

????@property

????def?birth(self):

????????return?dt.strptime(self._birth,?'%Y%m%d').strftime('%Y-%m-%d')

def?alignment(str1,?space,?align?=?'left'):

????length?=?len(str1.encode('gb2312'))

????space?=?space?-?length?if?space?=length?else?0

????if?align?==?'left':

????????str1?=?str1?+?'?'?*?space

????elif?align?==?'right':

????????str1?=?'?'*?space?+str1

????elif?align?==?'center':

????????str1?=?'?'?*?(space?//2)?+str1?+?'?'*?(space?-?space?//?2)

????return?str1

????

def?main():

????fname?=?'customer.txt'

????'''

????with?open(fname,?'w')?as?f:

????????f.write("""

????????郑文杰?225122198611134730

????????文萍?225122198912094740

????????郑妈妈??225122590303476

????????郑爸爸?225122560506471

????????""")

????'''????

????newf?=?'ourcustomers.txt'

????with?open(fname)?as?f:

????????s?=?f.readlines()

????L,?newL?=?[re.split(r'\s+',?i.strip())?for?i?in?s],?[]

????for?i?in?L:

????????if?len(i)?==?2:

????????????g?=?Idcard(i[1])

????????????newL.append('{}{}{}'.format(

????????????????alignment(i[0],?10),?alignment(g.sex,?8),?g.age))

????with?open(newf,?'w')?as?f:

????????f.write('\n'.join(newL))

????print('\n'.join(newL[:100]))

????print('Customer?data?has?been?write?into?{}'.format(newf))

if?__name__?==?'__main__':

????import?doctest

????doctest.testmod()

????main()

(责任编辑:IT教学网)

更多

推荐java认证文章