Python函数简单Demo案例

Python011

Python函数简单Demo案例,第1张

目录

一、写一个函数求三个数的和,并返回结果

二、写一个函数求三个数的平均值,并返回结果

三、再写一个函数求每个数与平均值之间的差,并返回结果

python简单的函数定义和用法实例

这篇文章主要介绍了python简单的函数定义和用法,实例分析了Python自定义函数及其使用方法,具有一定参考借鉴价值,需要的朋友可以参考下

具体分析如下:

这里定义了一个温度转换的函数及其用法。

def convertTemp(temp, scale):

if scale == "c":

return (temp - 32.0) * (5.0/9.0)

elif scale == "f":

return temp * 9.0/5.0 + 32

temp = int(input("Enter a temperature: "))

scale = input("Enter the scale to convert to: ")

converted = convertTemp(temp, scale)

print("The converted temp is: " + str(converted))

希望本文所述对大家的Python程序设计有所帮助。

本节中的万花筒通过彩色随机螺旋线来实现。我们首先定义一个函数draw(),这个函数用来绘制一个螺旋线,函数中的画笔起始位置的坐标为函数的两个形参。然后调用函数的时候使用一个for循环来实现多次调用函数,同时,函数的两个实参由random模块生成的随机数组成。