本文目录一览:
- 1、两道python的编程题求代码
- 2、Python编程题2--水仙花数
- 3、python 编程 求答案!2、3两题
- 4、一道简单的python编程题?
- 5、求一道python编程题
- 6、关于python程序设计题的题库?
两道python的编程题求代码
1,第一题:
def same_first_name(name1, name2):
if name1 is None or name2 is None:
print 'name1 or name2 is None'
elif type(name1) != type([]) or type(name2) != type([]):
print 'name1 or name2 no list'
elif len(name1) * len(name2) == 0:
print 'list name1 or list name2 is empty'
elif name1[0] == name2[0]:
return True
else:
return False
print same_first_name(['John', 'Smith'], ['John', 'Harkness'])
print same_first_name(['John', 'Smith'], ['Matt', 'Smith'])
运行结果:
True
False
2,第二题:
def search_closet(items, colour):
out_list = []
for item in items:
if colour in item.split(' '):
out_list.append(item)
return out_list
print search_closet(['red summer jacket', 'orange spring jacket', 'red shoes', 'green hat'], 'red')
运行结果:
['red summer jacket', 'red shoes']
3,说明:
第二题那个
if colour in item:
在我这运行成功的,如果你那不行就把把item按空格' '进行分割为一个list
其次,把第一题的参数判断的几个if else,你自己移到第二题上感受下.
Python编程题2--水仙花数
如果一个 3 位数等于其各位数字的立方和,则称这个数为水仙花数。
例如:153 = 1^3 + 5^3 + 3^3,因此 153 就是一个水仙花数
请按照从小到大的顺序输出 1000 以内的水仙花数(3位数),并用"逗号"分隔输出结果
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()
一道简单的python编程题?
按照题目要求编写的哥德巴赫猜想的Python程序如下
def IsPrime(v):
if v=2:
for i in range(2,v//2+1):
if v%i==0:
return False
else:
return True
else:
return False
n=int(input("输入一个正偶数:"))
if n2 and n%2==0:
for i in range(1,n//2+1):
if IsPrime(i)==True and IsPrime(n-i)==True:
print("%d=%d+%d" %(n,i,n-i))
else:
print("输入数据出错!")
源代码(注意源代码的缩进)
求一道python编程题
time="13时4分20秒"
i=time.find("时")
hour=time[:i]
j=time.find("分")
minute=time[i+1:j]
k=time.find("秒")
second=time[j+1:k]
print('{0:02s}:{1:02s}:{2:02s}'.format(hour,minute,second))
关于python程序设计题的题库?
1、average_sum函数的功能为求一批数中大于平均值
sum=0
k=0
for i in range(n):
sum=sum+a[i]
average=sum/n
for i in range:
if(a[i]average):
k=k+a[i]
return k
2、编写函数fun求一个不多于五位数的正整数的位数
if(m9999):
place=5
elif(m999):
place=4
elif(m99):
place=3
elif(m9):
place=2
else:
place=1
return place
3、请编fun函数,求4*4整形数组的主对角线元素的和
sum=0.0
for i in range(4):
sum+=a[i][i]
return sum
4、已知:一元钱一瓶汽水,喝完后两个空瓶换一瓶汽水。问:请输入钱数(大于1的正整数),则根据钱数最多可以喝到几瓶汽水。
s=0
k=0
while m0:
m=m-1
s=s+1
k=k+1
while k=2:
k=k-2
s=s+1
k=k+1
return s
5、编写函数fun(x,y),函数的功能是若x、y为奇数,求x到y之间的奇数和;若x、y为偶数,则求x到y之间的偶数和。要求必须使用for结构。
主函数的功能是分别计算如下的值:
(1+3+5+……+777)+(2+4+6+……+888)=???
(1+3+5+……+1111)+(2+4+6+……+2222)=???
(1+3+5+……+1999)+(2+4+6+……+1998)=???
s=0
for i in range(x,y+1,2):
s=s+i
return s
6、编写函数main 求3!+6!+9!+12!+15!+18!+21!
s=0
for i in range(3,22,3):
r=1
for j in range(1,i+1):
r*=j
s+=r
print(s)