day-05 python函数

# #-*- coding:utf-8 -*-
# 1:编写一个名为 make_shirt()的函数,它接受一个尺码以及要印到 T 恤上的字样。这个函数应打印一个句子,概要地说明 T 恤的尺码和字样。
def make_shirt(size,str_1):
print("T恤的尺码为:{} 字样为:{}".format(size,str_1))
make_shirt('m','余生都是你 ')

# 2:编写一个名为 describe_city()的函数,它接受一座城市的名字以及该城市所属的国家。这个函数应打印一个简单的句子,如 Reykjavik is in Iceland。
# 给用于存储国家的形参指定默认值。为三座不同的城市调用这个函数,且其中至少有一座城市不属于默认国家。
def describe_city(city,country="中国"):
print(city, "is in ",country)
describe_city('长沙')

# 3:编写一个名为 city_country()的函数,它接受城市的名称及其所属的国家。这个函数应返回一个格式类似于下面这样的字符串:
# "长沙, 中国"
# 至少使用三个城市国家对调用这个函数,并打印它返回的值。
def city_country(city,country="中国"):
print(city + ", "+country)
city_country('changsha')
# 4:编写一个名为 make_album()的函数,它创建一个描述音乐专辑的字典。这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。
# 使用这个函数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑的信息。
def make_album(singer,zhuanji):
dict={"歌手":singer,"专辑":zhuanji}
print(dict)
return dict
make_album('张信哲', '信仰')
make_album('徐誉滕', '等一分钟')
make_album('王玉萌', '浪子回头')

# 5:编写一个函数,它接受顾客要在三明治中添加的一系列食材。这个函数只有一个形参(它收集函数调用中提供的所有食材),并打印一条消息,
# 对顾客点的三明治进行概述。调用这个函数三次,每次都提供不同数量的实参。
def Sandwich_make(*args):
print(*args)
Sandwich_make('奶油')
Sandwich_make('奶油', '蛋糕')
Sandwich_make('奶油', '生菜', '蛋糕')

# 初级题型:
# 1:定义一个函数,成绩作为参数传入。如果成绩小于60,则输出不及格。如果成绩在60到80之间,则输出良好;如果成绩高于80分,则输出优秀,
# 如果成绩不在0-100之间,则输出 成绩输入错误。
def chengji(score_1):
score=input('请输入成绩')
if score.isdigit():
score_1=int(score)
if 0<score_1<=100:
if score_1 < 60:
print('不及格')
elif 60<score_1 <=80:
print('良好')
elif score>80:
print('优秀')
else:
print('成绩输入有误,请输入成绩在0-100之间')
else:
print('请重新输入成绩')
#
# chengji(77) # 调用函数
# 2:用函数实现:
# 企业发放的奖金根据利润提成。
# 利润(I)低于或等于10万元时,奖金可提10%;
# 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;
# 20万到40万之间时,高于20万元的部分,可提成5%;
# 40万到60万之间时高于40万元的部分,可提成3%;
# 60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,
# 从键盘输入当月利润I,求应发放奖金总数?
def bonus():
profit=input('请输入当月利润I')
if profit.isdigit():
profit_1=int(profit)
if profit_1<=100000:
# profit_1=profit_1*0.1
print('奖金可提10%,奖金为{}'.format(profit_1*0.1))
elif 100000<profit_1<200000:
print('奖金可提7.5%,奖金为{}'.format(profit_1*0.075))
elif 200000<=profit_1<400000:
print('奖金可提5%,奖金为{}'.format(profit_1 * 0.05))
elif 400000<=profit_1<600000:
print('奖金可提3%,奖金为{}'.format(profit_1 * 0.03))
elif 600000<=profit_1<1000000:
print('奖金可提1.5%,奖金为{}'.format(profit_1 * 0.015))
else:
print('奖金可提1%,奖金为{}'.format(profit_1 * 0.01))
else:
print('输入的利润有误,请重新输入')
# bonus() # 调用函数

# 3:用python函数实现如下:
# 随机产生一个数,让用户来猜,猜中结束,若猜错,则提示用户猜大或猜小。
import random
x=random.randint(1,10)
i=0
while i<10:
y=int(input("请输入数字"))
if x==y:
print("恭喜你猜对了")
break
elif y < x:
print("猜小了")
else: print("猜大了")

# # 4:写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5
def parameter(category):
if len(category)>5:
print("该参数长度大于5")
else:
print("该参数长度不大于5")
str_1="hello python"
list=[33,66,88,99]
tuple=("千里挑一","1257",7890)
parameter(str_1)
parameter(list)
parameter(tuple)
# # 5:写函数,将姓名、性别,城市作为参数,并且性别默认为f(女)。如果城市是在长沙并且性别为女,则输出姓名、性别、城市,并返回True,否则返回False。
def constitute(name,city,sex='f'):
name=''
# sex='f'
city=''
if sex=='f':
print('女')
if city=='长沙'and sex=='f':
print(name,sex,city,)
return True
else:
print('')
return False
constitute('zhao','女','中国')
# 6:写函数,检查传入列表的长度,如果大于2,那么仅仅保留前两个长度的内容,并将新内容返回
# def list(num):
# if len(num) > 2:
# a=num[2:]
# print(a)
# list([1,2,3,4,5,6,])
# 7:定义一个函数,传入一个字典和字符串,判断字符串是否为字典中的值,如果字符串不在字典中,则添加到字典中,并返回新的字典。
# def add_dict(dict_1,str_1):
# dict={'sex':'男','age':18}
# str_1='python'
# if str_1 not in dict.values():
# dict[str_1]= str_1
# else:
# print('已存在')
# return dict
#
# r=add_dict({'age':12,'sex':'hello'},'4')
# print(r)
#
# 中级题型:
# # 1:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
# for x in range(1,5):
# for y in range(1,5):
# for z in range(1,5):
# if (x!=y) and (y!=z) and (z!=x):
# print("%d%d%d" % (x, y, z))
# # 2:一个足球队在寻找年龄在m岁到n岁的男生or女生(包括m岁和n岁,到底是找男生还是女生,可指定性别)加入
sum=0
m=10
n=12
for i in range(0,10):
sex=str(input('请输入性别,m表示男性,f表示女生'))
age=int(input('请输入年龄'))
if (age>=10 and age<=12) and (sex=='f'):
sum+=1
print('恭喜你被录入了')
else:
print("请输入年龄10岁到12岁的女性")
print('总录入人数:'+str(sum))
# 。
# 写一个程序,询问用户的性别(m表示男性,f表示女性)和年龄,然后显示一条消息指出这个人是否可以加入球队,询问k次后,输出满足条件的总人数。
# count=0
# k=0
# while k<=3:
# sex=input('请问你的性别,m表示男性,f表示女性')
# if sex=='m':
# print('欢迎加入球队')
# count+=1
# k+=1
# else:
# print('很遗憾,你不能加入球队')
# print(count)
posted @ 2018-12-13 00:10  菩提306  阅读(657)  评论(0编辑  收藏  举报