Files
2025PY/day24/05-函数的案例.py
2025-05-22 16:50:44 +08:00

64 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 练习:
# 1.利用函数实现传入一个年份,判断是否是闰年
def is_run(year): # year:形参,占位
if year % 4 == 0 and year % 100 !=0 or year % 400 == 0:
return '闰年'
else:
return '非闰年'
# 调用三次函数
# print(is_run(2000))
# print(is_run(2001))
# print(is_run(2002))
# 2.利用函数实现两个数字的加减乘除
# 分析:参数
# def calc(n1,n2,op): # n1,n2都是数字op是操作符
# if op == '+':
# return n1 + n2
# elif op == '-':
# return n1 - n2
# elif op == '*':
# return n1 * n2
# elif op == '/':
# return n1 / n2
# else:
# return '输入有误!'
# print(calc(1,2,'+'))
# print(calc(1,2,'-'))
# print(calc(1,2,'*'))
# print(calc(1,2,'/'))
# 3.函数实现请输入一个km数值计算打车费用已知出租车起步价10块钱3km以内超出的部分2元/km
# def car_money(km):
# if km > 3:
# return 10 + (km-3)*2
# else:
# return 10
# print(car_money(5))
# print(car_money(2))
# print(car_money(10))
# 4.函数实现:根据公式(身高-108)*2 = 体重可以有10斤左右的浮动来观察测试者体重是否标准(身高cm)
def calc(height,weight):
max = (height - 108)*2 + 10 # 最大限度
min = (height - 108)*2 - 10 # 最小限度
if weight > max:
return '偏胖'
elif weight < min:
return '偏瘦'
elif min<=weight<=max:
return '标准身材'
else:
return '输入有误'
print(calc(170,180))
print(calc(170,110))
print(calc(170,130))