python - work4

# -*- coding:utf-8 -*-

'''
@project: jiaxy
@author: Jimmy
@file: work_20181108.py
@ide: PyCharm Community Edition
@time: 2018-11-08 16:01
@blog: https://www.cnblogs.com/gotesting/

'''
# 1:一个足球队在寻找年龄在x岁到y岁的小女孩(包括x岁和y岁)加入。
# 编写一个程序,询问用户的性别(m表示男性,f表示女性)和年龄,然后显示一条消息指出这个人是否可以加入球队,询问k次后,输出满足条件的总人数。
#

def join_footteam(x,y,k):
count = 0
for i in range(0,k):
sex = input('请输入性别:')
age = input('请输入年龄:')
if sex == 'f' and y >= int(age) >= x:
print('恭喜,您可以加入球队')
count += 1
else:
pass
return count
counts = join_footteam(12,14,10)
print('满足条件的总人数{}个'.format(counts))


#
# 2:写函数,
# 判断用户传入的对象(字符串、列表、元组)长度是否大于5 6、写函数,检查传入列表的长度,如果大于2,那么仅仅保留前两个长度的内容,并将新内容返回 
#

def get_len(args):
print('长度为{}'.format(len(args)))
if len(args) > 56:
print('{}的长度大于56'.format(args))
else:
print('{}的长度在56以下'.format(args))
get_len('12345678901234567890123456789012345678901234567890123456789012345678901234567890')
get_len([1,2,3,4,5,6,7,8,9,0])
get_len((1,2,3,4,5,6,7,8,9,0,1))


def get_list(args):
if len(args) > 2:
args = args[0:2]
else:
pass
return args

new_list1 = get_list([1])
new_list2 = get_list([1,2])
new_list3 = get_list([1,2,3])
new_list4 = get_list([1,2,3,4,5,6])

print('''
new_list1:{},
new_list2:{},
new_list3:{},
new_list4:{}
'''.format(new_list1,new_list2,new_list3,new_list4))
#
# 3、定义一个函数,
# 传入一个字典和字符串,判断字符串是否为字典中的值,如果字符串不在字典中,则添加到字典中,并返回新的字典。 

def str_into_dict(str,dict):
if str not in dict.values():
dict['new_str'] = str
else:
print('{}在{}中'.format(str,dict))
return dict

new_dict1 = str_into_dict('123456',{'name' : 'Jimmy','age' : 18, 'str':'123456'})
new_dict2 = str_into_dict('12345678',{'name' : 'Jimmy','age' : 18, 'str':'123456'})
print('''
new_dict1:{}
new_dict2:{}
'''.format(new_dict1,new_dict2))



 

 

 


posted @ 2018-11-08 17:02  JiaxyGogogo  阅读(219)  评论(0编辑  收藏  举报