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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

Python使用poplib模块和smtplib模块收发电子邮件的教程

poplib模块接收邮件 python的poplib模块是用来从pop3收取邮件的,也可以说它是处理邮件的第一步。 POP3协议并不复杂,它也是采用的一问一答式的方式,你向服务器发送一个...

Python实现的多项式拟合功能示例【基于matplotlib】

Python实现的多项式拟合功能示例【基于matplotlib】

本文实例讲述了Python实现的多项式拟合功能。分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- #! python2 import numpy as...

python itchat给指定联系人发消息的方法

itchat模块 官方参考文档:https://itchat.readthedocs.io/zh/latest/ 安装 pip install itchat / pip3 insta...

使用python turtle画高达

使用python turtle画高达

我就废话不多说了,直接上代码吧! import turtle t=turtle.Turtle() turtle.Turtle().screen.delay(0) tleft=turt...

Python中使用__new__实现单例模式并解析

单例模式是一个经典设计模式,简要的说,一个类的单例模式就是它只能被实例化一次,实例变量在第一次实例化时就已经固定。  在Python中常见的单例模式有None,这就是一个很典...