栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python暑期学习7.29

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python暑期学习7.29

def eat(somebody,something):
    print(somebody + '把' + something +'吃了')
eat('小甲鱼','蛋糕')
eat('蛋糕','小甲鱼')
eat(something='蛋糕',somebody='小甲鱼')

小甲鱼把蛋糕吃了
蛋糕把小甲鱼吃了
小甲鱼把蛋糕吃了

默认参数是函数默认的值

收集参数

def text(*params):
    print("有%d个参数"%len(params))
    print("第二个参数是:",params[1])
text('F','i','s','h','C')
text('小甲鱼',123,3.14)

有5个参数
第二个参数是: i
有3个参数
第二个参数是: 123

def discount(price,rate):
    final_price = price * rate
    return final_price
old_price = float(input('请输入原价:'))
rate = float(input("请输入折扣率:"))
new_price = discount(old_price,rate)
print('打折后价格是:%.2f' %new_price)
"""
price、rate和final_price都是局部变量
"""

请输入原价:80
请输入折扣率:0.75
打折后价格是:60.00

def discount(price,rate):
    final_price = price * rate
    print('试图在函数内部访问全局变量old_price的值:%.2f'%old_price)
    return final_price

old_price = float(input('请输入原价:'))
rate = float(input('请输入折扣率:'))
new_price = discount(old_price,rate)

print("打折后价格是:%.2f" %new_price)

请输入原价:80
请输入折扣率:0.75
试图在函数内部访问全局变量old_price的值:80.00
打折后价格是:60.00

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1029987.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号