Python 函数与变量--习题19,learn python the hard way

本文通过一个简单的Python函数实例,展示了如何定义和调用函数,并介绍了五种不同的参数传递方式,包括直接使用数字、变量、数学表达式、数字结合变量以及用户输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#coding=utf-8
def Total_number_of_students(boys, girls):
    print "There are %d boys within the environment." % boys
    print "There are %d girls within the environment.\n" % girls
#运行方式一:数字
print "Here's the most simple way to operate this function by using numbers"
Total_number_of_students(4, 5)
#运行方式二:变量
print "Here's another way to make the function works by working with arguments."
boys = 10
girls = 20
Total_number_of_students(boys, girls)
#运行方式三: 数学表达式
print "Here's another way to import numbers to functions"
Total_number_of_students(10 + 3, 10 + 4)
#运行方式四:数字+变量
print "Here's another way to make the function works."
Total_number_of_students(boys + 4, girls + 5)
#运行方式五:用户输入
print "Here's the fifth way to make the function meaningful by raw_input:"
boys = int(raw_input("how many boys are there within the environment?"))
girls = int(raw_input("how many girls are there within the envrionment?"))
Total_number_of_students(boys, girls)
#运行方式六:用户输入2

Note:
1)def函数名是由:字符与下划线组成的。
2)函数名后面紧跟着()
3)()里面包含着参数,多个参数用逗号隔开,切不能使用重复的参数名。
4)函数名后面的(),紧跟着:
5)4个空格缩进后,编写该函数的代码。
6)用户在终端输入时的数字也是字符串,应该使用X = int(raw_input()) 来处理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值